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?
↧