C# 无法将公共变量的值获取到Unity3D c中的另一个脚本中#

C# 无法将公共变量的值获取到Unity3D c中的另一个脚本中#,c#,unity3d,C#,Unity3d,在我的gameController.cs脚本中有一个public Color winColorvar。我正在Start()中设置它的值。现在我想在另一个脚本check.cs中获取它的值 现在我使用了GameObject.Find(“gameController”).GetComponent().winColor 这里的问题是,它显示了不同的值。 这是我在tile.cs private Color winingColor; void Start () { win

在我的
gameController.cs
脚本中有一个
public Color winColor
var。我正在
Start()
中设置它的值。现在我想在另一个脚本
check.cs
中获取它的值

现在我使用了
GameObject.Find(“gameController”).GetComponent().winColor
这里的问题是,它显示了不同的值。
这是我在
tile.cs

private Color winingColor;

    void Start () 
    {
        winingColor = GameObject.Find("gameController").GetComponent<gamePlay>().winColor;
        Debug.Log(winingColor);
    }

    void Update () 
    {
        Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        bool overSprite = this.GetComponent<SpriteRenderer>().bounds.Contains(mousePosition);

        if (overSprite)
        {
            if (Input.GetButton("Fire1"))
            {

                if (this.GetComponent<SpriteRenderer>().color == winingColor)
                {
                    float x = this.gameObject.transform.position.x;
                    this.gameObject.transform.position = new Vector3(x, 3.5f, 0.0f);
                }
            }
        }
    }
gameController.cs
winColor
的值是
RGBA(1.000,0.000,0.000,1.000)
但是在
tile.cs
中,我得到了
RGBA(0.000,0.000,0.000,0.000)

有什么想法吗?

不同游戏对象的开始()顺序是你意想不到的。
如果首先发生Tile.cs Start(),则不会设置winColor

将此行移到Awake()中

解决这个问题的另一种方法是,如果winColor不应该更改,可以将winColor更改为属性getter,并删除
winColor=colors[1]

public Color winColor { get { return colors[1];}}

在我的实验中,它起作用了,你确定你没有在代码的某个地方改变颜色吗

测试2:

public class test2 : MonoBehaviour
{
    public Color winColor;
    private Color[] colors = { new Color(0, 1, 0, 1), new Color(1, 0, 0, 1), new Color(1, 1, 1, 1), new Color(0, 0, 1, 1), new Color(1, 1, 0, 1), new Color(0, 0, 0, 1) };

    // Start is called before the first frame update
    void Start()
    {
        winColor = colors[1];
        Debug.Log("con wincolor:" + winColor);
    }

}
测试1:

public class test1 : MonoBehaviour
{
    private Color winingColor;

    // Start is called before the first frame update
    void Start()
    {
        winingColor = GameObject.Find("test").GetComponent<test2>().winColor;
        Debug.Log("test1:" + winingColor);
    }    
}

你能发布你的游戏控制器代码吗?添加了
gameController
code是
gameController.cs
中称为gameController或gamePlay的类吗?
GameObject.Find(“gameController”).getComponent()行中的函数
getComponent
正在尝试获取一个名为
gamePlay
的类,您应该检查
getComponent
调用中的type参数是否对应于
gameController.cs
文件中的类可能没有winColor的副本会更改这只是我项目的开始。但是是的,我忘了使用Awake()。它起作用了。
public class test2 : MonoBehaviour
{
    public Color winColor;
    private Color[] colors = { new Color(0, 1, 0, 1), new Color(1, 0, 0, 1), new Color(1, 1, 1, 1), new Color(0, 0, 1, 1), new Color(1, 1, 0, 1), new Color(0, 0, 0, 1) };

    // Start is called before the first frame update
    void Start()
    {
        winColor = colors[1];
        Debug.Log("con wincolor:" + winColor);
    }

}
public class test1 : MonoBehaviour
{
    private Color winingColor;

    // Start is called before the first frame update
    void Start()
    {
        winingColor = GameObject.Find("test").GetComponent<test2>().winColor;
        Debug.Log("test1:" + winingColor);
    }    
}
public class test2 : MonoBehaviour
{
    private Color[] colors = { new Color(0, 1, 0, 1), new Color(1, 0, 0, 1), new Color(1, 1, 1, 1), new Color(0, 0, 1, 1), new Color(1, 1, 0, 1), new Color(0, 0, 0, 1) };

    public Color winColor
    {
        get; set;
    }

    // Start is called before the first frame update
    void Start()
    {
        winColor = colors[1];
        Debug.Log("con wincolor:" + winColor);
    }

}