# Public value null after assigning it from a different script

Hello I am a beginner in Unity and still currently exploring with scripts. So I ran into a problem.

In FetchAndAssign.cs Script

    void Start(){

GameObject theEdge = GameObject.Find("Edge2");
edge._id = _id;

}


In StallComponentAdder.cs Script where it is attached to Edge2 GameObject

    public string _id;

void Start () {
string storeID = _id;
print(storeID);
}


When I try to print the storeID it won't print anything, why is that so?

• I think the Start() function of StaleComponentAdder.cs script is called than the Start() function of FetchAndAssign.cs called. Change Start() of FetchAndAssign.cs to Awake (). This function is called before start(). Aug 10, 2018 at 13:03
• What exactly is null? theEdge, edge , or _id? Aug 10, 2018 at 15:38

The problem you’re running into is likely due to an incorrect script execution order. The Start method on all scripts with the same priority which are loaded at the same time are executed in an arbitrary order. Your FetchAndAssign.Start is probably running afterStallComponentAdder.Start.

You can address this by either adjusting the script execution order to ensure that StallComponentAdder is run later, or you could change Start inside of FetchAndAssign to use Awake instead, which runs before Start.

• it is null when I print it but in the Unity Inspector I can see ID it is set to a value, the value that I assigned in the other script. And I believe my script order is correct because first it assigns to the unity inspector. Second, I Debug.Log both scripts and FetchAndAssign prints first. Aug 10, 2018 at 13:27
• I already try change it to awake in the fetchandassign.cs but still wont work Aug 10, 2018 at 13:30
• Try adding a print(_id); inside of FetchAndAssign.Start and see which one gets printed first. Also, I assume your problem is that the other call to print is just printing an empty string, right? Or is it not getting called at all? Aug 10, 2018 at 13:51

Others have answered this already, but it's caused due to execution order - the start of your adder is called before the assign.

Personally, I'd recommend doing the opposite - instead of setting it through the parent (findandassign), find and get the data through the adder, and set it internally. this way, if the data isn't yet set, you know immediately, and don't assume it is set. Now I can't see your code, so there's likely a reason you do it this way.

If that's the case, I'd suggest using Awake() over Start(). if it helps, Awake is more of a "After Initializing," and Start is more of a "Before First Update." You cannot tell what order things will be initialized in, and similarly, you can't tell what order everything will set itself before the first update. however, you are guaranteed that everything declared statically in a scene will be initialized during Awake, and everything will have called Awake before any one of them run Start. this means that you can set data in findAndAssign's Awake(), and it will be guaranteed to be set before anything runs Start().

Quick disclaimer: any class declared during runtime will still follow these rules, (Initialize -> Awake -> Start (if enabled)) but when during the frame they do so is more complicated.

Start() of StallComponentAdder is called earlier than Start() of FetchAndAssign script. You could do something like this:

FetchAndAssign.cs

void Start(){
GameObject theEdge = GameObject.Find("Edge2");
edge._id = _id; //assign
edge.StartAlt(); //call
}


public string _id;