0
\$\begingroup\$

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");
         StallComponentAdder edge = theEdge.GetComponent<StallComponentAdder>();
         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?

\$\endgroup\$
  • \$\begingroup\$ 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(). \$\endgroup\$ – coder_86 Aug 10 '18 at 13:03
  • \$\begingroup\$ What exactly is null? theEdge, edge , or _id? \$\endgroup\$ – return true Aug 10 '18 at 15:38
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Addy Pete Aug 10 '18 at 13:27
  • \$\begingroup\$ I already try change it to awake in the fetchandassign.cs but still wont work \$\endgroup\$ – Addy Pete Aug 10 '18 at 13:30
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – Ed Marty Aug 10 '18 at 13:51
1
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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");
     StallComponentAdder edge = theEdge.GetComponent<StallComponentAdder>();
     edge._id = _id; //assign
     edge.StartAlt(); //call
}

StallComponentAdder.cs

public string _id;

public void StartAlt () {
     string storeID = _id;
     print(storeID);
}

So that you know that StartAlt() is 100% called after _id is assigned :) hope this helps.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.