- Home /
Script initialization when instancing a game object
Hi there! I have a problem here with my code which is making me scratch my head, so I've decided to come here and ask for help: I have a prefab composed of several game objects. One of them has 4 C# script objects attached (A, B, C and D).
Script A initializes its variables inside its own Start method. In the Update function, it occasionally calls method X located inside Script B.
Script B also initializes its variables inside its own Start method, which are needed in order to properly execute method X.
Now, for some reason Script A is calling method ScriptB::X before Script::Start has even executed, which causes a null reference exception.
Now I've checked the documentation and I think I'm not doing anything illegal here. Can anyone help me with this?
Thanks in advance!
--Nacho
Without seeing the scripts it is had to say. So long as your method X isn't being called in start. Here are some debugging suggestions:
Log that you have exited each Start() method
Log that you have entered each Update()
Walk through the code in mono's debugger to see that Yes, script A starts up ad Yes so does script B
If anything in either script is set to null, then log when this occurs (and maybe log when it's instantiated).
Check the Console for when your null exception is occurring and you should see what happened around it - did your Start() methods succeed?
Off the top of my head I wonder if:
Your reference to script B in A and visa versa are null rather than the objects in side method X?
Whether one of your scripts is attached to a GO that is not active, in which case Start() won't be called for your script and everything really WILL be null - adding some logging as above may point to this sort of thing.
Cheers H
Thanks for your reply! I logged in that information and I can confirm that things occur in the following order: 1) Script A's Start method is properly executed. 2) Script A's Update method is executed (which calls method X from Script B and causes a null reference exception). 3) Script B's Start method is properly executed.
Both scripts are attached to the same object, so I don't get why is Script B's Start method executed after Script A's Update method and not before. Let me know if you need further information.
Thanks!
That seems very odd. You should check out the execution order of the scripts in question:
Edit -> Project Settings -> Execution Order
But I confess I thought all Start() methods occurred before Updates()...
As another suggestion, you might want to set things up in Awake(). This would be a great place to "make" things, just don't expect any other scripts to be Awake() yet, though in my experience it is fine to GetComponent() on the GO for the active script so you can store things like the Transform etc.. locally.
Once again, thanks for your reply! I haven't changed the scripts execution order (besides, the scripts are inside a game object which belongs to a prefab that is instantiated at run-time).
In any case, setting things up in Awake made the trick, so I can move on!
Thanks again for your help!
Also check if you could use Awake ins$$anonymous$$d of Start, as Awake comes before Start. I think Start is when the objects are in the hierachy, where as Awake is when the constructor is called?