thirteen4054
thirteen4054

Reputation: 485

Unable to get value of public variable into another script in Unity3D c#

I have a public Color winColor var in my gameController.cs script. I am setting its value in the Start(). Now I want to get its value in another script check.cs.

Now I since it is public I have used GameObject.Find("gameController").GetComponent<gamePlay>().winColor; The issue here is that it is displaying a different value. This is my code in 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.csCode

public GameObject ball;
    public List<GameObject> tiles;

    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;

    // Use this for initialization
    void Start () 
    {
        winColor = colors[1];
        Debug.Log("con wincolor:" + winColor);
        ball.GetComponent<SpriteRenderer>().color = colors[1];
        tiles[0].GetComponent<SpriteRenderer>().color = colors[0];
        tiles[1].GetComponent<SpriteRenderer>().color = colors[1];
        tiles[2].GetComponent<SpriteRenderer>().color = colors[3];
        tiles[3].GetComponent<SpriteRenderer>().color = colors[4];

    }

The value of winColor in gameController.cs is RGBA(1.000, 0.000, 0.000, 1.000) But in tile.cs I am getting RGBA(0.000, 0.000, 0.000, 0.000)

Any thoughts?

Upvotes: 1

Views: 86

Answers (2)

Chico3001
Chico3001

Reputation: 1963

In my experiment it is working, are you sure you are not changing the color somewhere on the code?

Test2:

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);
    }

}

Test1:

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);
    }    
}

Also in my opinion it woul be better to use Getters/Setters to access public propierties, they give you better flexibility:

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);
    }

}

Upvotes: 0

Leo Bartkus
Leo Bartkus

Reputation: 1945

Start() of different game objects is happening in the order you don't expect.
If Tile.cs Start() happens first, winColor won't be set yet.

Move this line into Awake()

winColor = colors[1];

Another way you could solve this, if winColor isn't supposed to change, is you could change winColor to a Property getter, and remove the winColor = colors[1]; line.

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

Upvotes: 1

Related Questions