c# - Unity 中其他脚本中的变量

标签 c# variables unity3d scripting

我正在尝试在 Unity 2d 中创建游戏。我已经完成了大部分我想做的事情,并开始处理敌人。敌人(龙)从屏幕的不同点进来。为此,我将 sprite 游戏对象放置在我希望龙生成的位置。我已将所有这些对象设为另一个名为 DragonAncores 的对象的子对象。我在 DragonAncores 上附加了一个脚本,上面写着......

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragonTracker : MonoBehaviour {
    // is gold dragon in play?
    public bool GoldDragonInit = false;
    // curently active dragons
    public int DragonCount = 0;
    // defalts to 5
    public int Difficulty = 5;
}

然后我将一个脚本附加到每个 Sprite ,最终将在龙预制件(包含 2 个碰撞器和一个动画器)中召唤,该龙预制件偏向于从其他变量派生的 If 语句逻辑。

下面是我正在使用的代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Dragons : MonoBehaviour {
    // same as GoldDragonInit
    bool GoldDragonSpawn = false;
    // same number as DragonCount in DragonTrackeer
    int LiveDragons;
    // same as Difficulty
    int DifLev;
    //get cariables from other script
    DragonAncors.cs.GetComponent.<DragonTracker>() GoldDragonInit = GoldDragonSpawn;

    System.Random RNG= new System.Random();
    void update()
    {
        RSpawn=RNG.Next(0,2)
        DragonType=RNG.Next(0,101)
        if (RSpawn = 1) ;
        {
            if (LiveDragons > DifLev) ;
            {
                if (DragonType > 99) ;
                {
                    // summon regular dragon
                }
                if (DragonType = 100) ;
                {
                    if (GoldDragonSpawn = true) ;
                    {
                        // summon gold dragon
                    }
                }
            }
        }
    }
}

这是抛出了这个错误列表。

Error List

这显示了我在统一中的层次结构和 anchor (Squair 十字准线看起来的东西)

Unity Hierarchy and anchor points

我已经寻找了解决此主题的其他线程,他们都尝试了不同的方法,但都没有用。

我使用的是Unity 2018.2.18f1

最佳答案

您的代码中存在一些错误。以下是不正确的。

//get cariables from other script
    DragonAncors.cs.GetComponent.<DragonTracker>() GoldDragonInit = GoldDragonSpawn;

访问它的正确方法,如您所说,DragonAncors 是父项:

GetComponentInParent<DragonTracker>().GoldDragonInit = GoldDragonSpawn;

这会将 GoldDragonInit bool 值设置为 GoldDragonSpawn 的值。这必须在一个函数内部,因为你在函数外部有它我假设你在开始时需要这个集合。因此我将它放在 void Start() 函数中。这在游戏开始时调用(加载场景)。

你也不需要分号;在 if 语句之后,但是它确实需要出现在每一行定义代码之后。您提供的代码应该如下所示。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Dragons : MonoBehaviour {
    // same as GoldDragonInit
    bool GoldDragonSpawn = false;
    // same number as DragonCount in DragonTrackeer
    int LiveDragons;
    // same as Difficulty
    int DifLev;
    void Start()
    {
        // variables from other script
     GetComponentInParent<DragonTracker>().GoldDragonInit = GoldDragonSpawn;
    }
    System.Random RNG= new System.Random();
    void update()
    {
        RSpawn=RNG.Next(0,2);
        DragonType=RNG.Next(0,101);
        if (RSpawn = 1)
        {
            if (LiveDragons > DifLev)
            {
                if (DragonType > 99)
                {
                    // summon regular dragon
                }
                if (DragonType = 100)
                {
                    if (GoldDragonSpawn = true)
                    {
                        // summon gold dragon
                    }
                }
            }
        }
    }
}

这是有效的,因为 DragonTracker 是父对象中的一个脚本。如果不是这种情况,则 GetComponentInParent().GoldDragonInit = GoldDragonSpawn;会像这样被替换:

[SerializeField]
private GameObject DragonAncors;
void Start()
{
    DragonAncors.GetComponent<DragonTracker>().GoldDragonInit = GoldDragonSpawn;
}

关于c# - Unity 中其他脚本中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55066284/

相关文章:

Javascript 作用域和局部变量

c++ - 解析 GLSL 着色器字符串以在 Android NDK 中查找变量名称

c# - Unity - 在没有物理的情况下将变换移动到特定位置(以特定速度)的最佳方法?

c# - 在 C# 中使用反射向对象添加属性

java - Log4J - J2EE - 动态变量

c# - 将预制件统一设置为另一个游戏对象的子对象

ios - 无法在 iOS 上从 Unity 运行 Vuforia 应用程序。代码签名无效

c# - 检测您的 Windows 8 应用程序何时被卸载?

c# - 使用 "greater than or equals"或仅使用 "greater than"

c# - C# 库读取 Excel 2007 xlsm 文件?