Get value of variable of one defined object C#

Hello,

I know there are many answered questions about this, but I still can´t get it to work and I know I am close!! I tried different methods like the GetComponent but to many error. I think that if this works then is the easiest, the problem is that now I don´t get better and now I am always reading same posts and can´t get better, so please help!!

The example: I have “Main” object and 2 secondary objects with different names “Secondary1” and “Secondary2”. I apply same scripts to Secondary objects, this script gives a random number which I make public to have access. Then the objective is make a script for the “Main” object that gets the random values of the Secondary objects.

Script for Secondary objects:

public class Secondary: MonoBehaviour {

	public int num;

	void Update () {

		num = Random.Range(0, 4);

        }
}

Script for “Main” object:

public class MainsScript : MonoBehaviour {

	int num1;
	int num2;


	void Update () {


		num1 = GameObject.Find ("Secondary1").GetComponent<Secondary> ().num;
		num2 = GameObject.Find ("Secondary2").GetComponent<Secondary> ().num;

    }

Please notice I am a newbie, code and explanation will help me learn where am I always failing :wink: Thank you!!

Try this, to MainScript add

private Secondary sec

to Start() add

sec = GameObject.Find("Secondary1").GetComponent<Secondary>();

in Update try

num1 = sec.num;