question about operating on an instantiated object

Hi all,

I am working my way through some tutorials on working in unity with C# and I have a question about the following code:

//Instantiate a new muzzleFlashPrefab object and store in the variable clone
Transform clone = Instantiate (muzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;

// set the new objects parent to firepoint.
clone.parent = firePoint;

//create a random float and use this as the size of clone
float size = Random.Range (0.3f, 0.6f);
clone.localScale = new Vector3 (size, size, size);

//Then destroy the muzzleFlash
Destroy (clone.gameObject, 0.02f);

My question is this. Obviously the code randomises the size of the “muzzle flash” but it does so after the object has been instantiated. Now this codes works but I am wondering if anyone can tell me why? Surely if the object has been already been instantiated at default size this shouldn’t do anything? Or am I instantiating it (so it appears on the screen) then changing the size so quickly I don’t notice the original size before deleting it?

Apologies if this is a silly question I feel like I’m missing something here.

This is a valid question and an important concept to understand early. Two points worth making here:

Everything in computer science happens sequentially, and drawing to the screen is the last thing that occurs in a given frame. This is not the most technically accurate way to explain this idea, but it’s true until you get into things like multi-threading or post-processing effects.

So you can create an object, and in the next few lines, change its size 5000 times in a loop, and what you’ll actually see on the screen is the last change made. In other words, the five-thousandth change is the one drawn to the screen.

There’s nothing wrong with making changes to an object (its prefab) before that object is instantiated, but it’s generally not very intuitive to do that. Make your object, make your changes, then it’s drawn to the screen at the end of the frame. Hope that clarifies things a bit!