Guillem Poy
Guillem Poy

Reputation: 811

Execute an script's Start() after another script's Start() Unity C#

I would like to run the Start() from one script just before the Start() of another Script. Is it possible? Can you choose the order of the executions of the scripts?

Upvotes: 5

Views: 10153

Answers (4)

jarr
jarr

Reputation: 102

The best solution for this is to use C# Invoke. Create an Invoke event that will be triggered after the main class have been initialized i.e. At the end of its Start(). Have the dependent class subscribe to this event on its Start(). That way once your main instance it's finished, it will signal the dependent instance that it can now go to initialize.

For example, you have a Player, GameMode, UI instance. The UI depends on the Player. Have UI subscribe to an event that's invoked by the Player or GameMode class after the Player instance is created. Once the Player instance is created, UI will receive the signal that it's now free to do its thing.

And you can use this Event/Invoke for any other class instance that requires the player or any other. And they are easy to track.

Upvotes: 1

Ally. M
Ally. M

Reputation: 1

What you may be able to do is normally do the script that you are wanting first however you would like it. However lets say your wanting to run the other script at the end of the first script, You may be able to reference a function by using this (Replacing SecondScriptName with the script you want to go after the first one then replacing FunctionFromSecondScript with the Function from that script)

  <SecondScriptName>().FunctionFromSecondScript();

You can then call all of the functions in turn in whatever order you wish.

If i make a mistake, Please forgive me as this is my first comment to help another programmer and I am currently a budding one myself.

Hope this helps :)

Upvotes: -1

Everts
Everts

Reputation: 10721

If you have one script (A) meant to run after another (B), I guess it means A depends on B. In that case, you should get B to call for A passing the needed data.

public class A : MonoBehaviour
{
     public void Init(State state){}
}

public class B : MonoBehaviour
{
     private State state;
     void Start()
     {
          this.state = SetState();
          this.gameObject.GetComponent<A>().Init(this.state);
     }
}

This might be the only way in the long run preventing long debugging hours. In fact, if you use the script execution order, it is fine until you have a lot of classes and you have been working on the project for 6 months or more. Worst, you give the project to another coder. Then you have "invisible" dependencies with new bugs you can hardly debug since they are not in the code.

Upvotes: 3

Kay
Kay

Reputation: 13146

I am not totally sure about Start() but you can configure the Script Execution Order of Awake, OnEnable and Update. Go to menu Edit / Project Settings and set your preferences like described in the manual section. So you might want to investigate further if Start is affected too - I believe it is as it is kind of related to Update

In general I would recommend to use this feature carefully. If you run nto the situation of having too many scripts in this list, this indicates some design issues.

Upvotes: 7

Related Questions