I want to call a certain method, contained inside a script(´scriptA´) , and I want to call this method from another script(´ScriptB´)´s awake function.
Both of the scripts are MonoBehaviours attached to separate GameObjects, in separate scenes.
Have I described this well enough? If so, could someone please give me a hint/help to achieve this?
Best practice is to avoid calling external code from Awake because in Awake there is no guarantee that the other object is available and completely set up because it may or may not have run its own Awake yet.
You didn’t say whether A is part of the same game object that B is on. If so, then that would not make it an “external” script in the above sense but it’s still best practice to use Awake only to get a reference and use Start to make initialization calls on it:
//[SerializeField]
private ScriptA scriptA;
void Awake() => scriptA = GetComponent<ScriptA>();
void Start() => scriptA.TheMethodToCall();
Now if scriptA isn’t on the same game object, you would uncomment the commented line and remove the GetComponent in Awake. Then you only need to drag the game object with ScriptA on it onto the field in the Inspector that you’ll see when you select the object with ScriptB on it.
Inspector references are the way to easily link objects together but note that there are situations where this won’t work, eg when Instantiating objects or when the object is a prefab which can’t have Inspector references to scene objects.
1 Like
Thank you for helping, It Really helped