|
I'm having trouble trying to change values in another script, and I'm clueless as to why. The code below is in the Start function of a script attached to the main camera: When running: I can see the object has been initialised in the hierarchy, but the test variable is set to "Bad" (which is set in the Start() method of the tileEngineTile script) in the inspector. None of the other methods/variables I've written work either, and I haven't got a clue as to why. The test variable as well as the other methods are all public, and I haven't got a clue as to what's going on. Does anyone have any ideas?
(comments are locked)
|
|
The start method is called the next frame after you instantiated the object. So it's overwriting any change you do right after the instantiation. Awake is called immediately when the object got created / instatiated. Awake is executed even before the Instatiate function returns. Do local initialization which doesn't require external references in Awake or direct as field initializer like so: Start should be used when you want to access other objects / components since it's guaranteed they are available and created when Start is called. The Start-function-calling is part of the main game loop. If an object has been created in the last frame the Start method is called at the beginning of the next frame.
(comments are locked)
|
|
The Start() method of the newly instantiated object is executed at some point after the Start() method of the main camera's component. It's not executed as part of the instantiation. Hence, what happens is that the object is instantiated, then its member variable is set to "Good", and then its own Start() method sets the variable back to "Bad".
(comments are locked)
|
