Cannot read a random integer from another script.
Hey, I've been fiddling with Unity for a while know, learning to code myself through a lot of tutorials and pasting/re-writing/modyfing them to my needs. I decided its time to make something small but without help of any "outside" code, just from the docs and mind. And it made me stuck on this particular problem, i cannot read random generated integers from another script, they keep popping as 0 (zero) all the time, if you could tell me where I made my mistake I would be greatful, as for now, i've been searching for the answear on the forums+google but nothings seems to fix it, its eaither compilation error or the same outcome as it is now, I even re-wrote this thing using different methods. Here is what i've got:
The first random number generator, that also pops the number into UI text box
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class RandomNumberA : MonoBehaviour {
public int a;
Text numA;
void Awake ()
{
numA = GetComponent <Text> ();
}
void Start () {
int a = Random.Range(1,255);
numA.text = "" +a;
}
void Update () {
}
}
There is another one that looks the same, but with int B, but then there is one script that should add both and make it a C
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
//using RandomNumberA;
//using RandomNumberB;
public class Add : MonoBehaviour {
private int anum;
private int bnum;
public int c;
Text addnum;
void Awake ()
{
addnum = GetComponent <Text>();
}
// Use this for initialization
void Start ()
{
anum = GameObject.Find("VarA").GetComponent<RandomNumberA>().a;
print ("A" +anum);
bnum = GameObject.Find("VarB").GetComponent<RandomNumberB>().b;
print ("B" +bnum);
c = anum + bnum;
print(c);
}
// Update is called once per frame
void Update ()
{
}
}
int a is on game object in canvas called VarA the b is on the same Canvas called VarB
Console pops me with this:
A0 UnityEngine.MonoBehaviour:print(Object) Add:Start() (at Assets/Add.cs:22)
B0 UnityEngine.MonoBehaviour:print(Object) Add:Start() (at Assets/Add.cs:24)
0 UnityEngine.MonoBehaviour:print(Object) Add:Start() (at Assets/Add.cs:26)
Answer by tpusch · Dec 02, 2015 at 10:26 PM
The problem is that you are re-declaring the variable a, and I assume b in your Start() method. When you type int a = Random.Range(1,255)
again it creates a new variable a
that is local only to the start method and does not change your class member a. So instead just assign a random number to the a that you already created.
public int a;
void Start () {
a = Random.Range(1,255);
numA.text = "" +a;
}
I did what you suggested, its still the same, nothing changed.
Ok, then the next step I would suggest would be assigning the value of your random a and b in awake ins$$anonymous$$d of start, since your Add script may be running its start before the RandomNumberA script.
Here is the manual for the order of unity functions: http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html for extra info.
Your answer
Follow this Question
Related Questions
i try make enemy health slider or any uI just show the health of Enemy Over head 0 Answers
UI Dropdown problem. 1 Answer
NullReferenceException on Text element 1 Answer
continously spanws and overlaps 0 Answers
Highscore - BestTime 1 Answer