1

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?

4
  • Can you post your gameController code? Commented Apr 9, 2019 at 14:24
  • Added the gameController code Commented Apr 9, 2019 at 14:30
  • 1
    Is the class in gameController.cs called gameController or gamePlay? The function getComponent in the line GameObject.Find("gameController").GetComponent<gamePlay>().winColor is trying to get a class called gamePlay, you should check that the type parameter in the getComponent call corresponds to the class in the gameController.cs file Commented Apr 9, 2019 at 14:40
  • Possible duplicate of Accessing a variable from another script C# Commented Apr 9, 2019 at 14:42

2 Answers 2

1

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];}}
Sign up to request clarification or add additional context in comments.

1 Comment

No winColor will change this is just the start of my project. But yes I forgot to use Awake(). It works.
0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.