Setting variables after instantiate

Im adding some prefabs with scripts attached and i want to alter a variable in the prefab script after ive spawned it.

For eg

newbot= Instantiate(P_HerdBot, transform.position, transform.rotation) as GameObject;
newbot.transform.parent = transform;//set to me as parent in heirarchy
newbot.transform.localPosition = new Vector3(200+Random.value * 50,100,200+Random.value * 50) ;//set local pos
C_HerdBot _C_HerdBot=newbot.GetComponent<C_HerdBot>();//get script
_C_HerdBot.m_Following=-1;//update var

This doesnt work for some reason.
I can change the variable in the same way but from my update function (ie after the creation function has run)
Is there a way around this?

This should work fine.

It could be working perfectly, but another piece of code is Zeroing that variable…awake() start() or update() etc.
My first thought would be the actual C_HerdBot script doing it, since Unity doesn’t execute scripts concurrently.

Ah ok, so when is start executed?
If its exectued on instantiate then i should be fine.
Sounds like it isnt though, does it wait until the script has exited?

"since Unity doesn’t execute scripts concurrently. " < ah, doh, ok best not set things in start then :slight_smile:

Awake() should be executed when its Instantiated (I’m about 99.9% sure)

Start() will probably be executed after the current script is finished, or whenever Unity gets round to it. :stuck_out_tongue:

My understanding is that Awake() is invoked during the call to Instantiate(), but that Start() can be executed pretty much any time, as long as it’s prior to the first invocation of Update() for that object (so, pretty much as callahan.44 said).

I’m pretty sure I’ve had cases where an object that did some rendering setup in Start() was instantiated, and the rendering setup didn’t actually take effect until the second frame that the object was rendered, which would indicate that Start() can actually be called on the next update cycle after an object is instantiated in some cases (or maybe all cases).