I have a script here that I’m using for a little game I’m making where the player squishes mosquitos. This script is designed to destroy a prefab and replace it with a pair of new prefabs, and I made it a couple days ago by pulling some bits of code from various tutorials and guides. It works fine, but now that I’m looking back over it, it doesn’t make sense and now I’m confused again.

The code looks like this:

var bloodSplatters : GameObject[]; //Creates an array that stores all bloodSplatter objects
var deadSkeeter : GameObject;

function OnMouseDown () {
	//Defines a variable that creates a random rotation on the Z axis
	var randomZRotation = Quaternion.Euler(0,0,Random.Range(0,360));
	
	//Defines a random variable as a picker for the bloodSplatters
	var bloodSplatterIndex = Random.Range(0,9);

	//Instantiates a blood splatter and a dead mosquito and destroys the "live" mosquito
	var numRandom : float = (Random.Range(0.8,1.2));
	var objectMade : GameObject = Instantiate (bloodSplatters[bloodSplatterIndex], transform.position, randomZRotation);
	objectMade.transform.localScale = Vector3.one * numRandom;
	Instantiate (deadSkeeter, transform.position, randomZRotation);
	Destroy (gameObject);
}

This is attached to the prefab that I’m destroying. I have 9 “bloodSplatter” objects stored in the array declared at the top. What I realized (I’m really new, should have seen this immediately I suppose, but I didn’t) when I looked back at the code is that “randomZRotation” and “bloodSplatterIndex” are just initialized but the type is never declared. Then I figured maybe they just default to a type based on what they’re initialized as, but Random.Range should return a float and I intended for “bloodSplatterIndex” to be of type int, and I never did any sort of rounding to clean up generated float values.

Now I want to clean up the code, but I don’t want to break my working script, and I’d like better understanding of what I’m doing before I move on. Help?

It just guesses what it should be by what you are doing on it later on.
Not a good thing to do. Just round it off and declare it as you wanted.

The type is declared, it’s just done implicitly by looking at the value. These statements are 100% identical:

var foo = 5;
var foo : int = 5;

Since “5” is an int, foo becomes an int. If you used “5.0” or “5.0f”, then foo would become a float. In both cases the type is statically declared and can’t be changed later…this has nothing to do with dynamic typing, so #pragma strict has no effect, and C# has the same feature:

var foo = 5;
int foo = 5;