Instantiate Object with Specific Values

Hi Everyone,

I’ve been trying for a while now to figure this out but I’m stumped. I’m trying to instantiate a group of objects each with random values for certain properties (Ex. larger scale, faster move speed, etc). This script works for the most part but the values only get increased on the original object, not any of the instances. If I run this script as is, I get a NullReferenceExpection error. The issue is how to access the variables in instanced objects individually, rather than globally.

//Variables for Minimun and Maximum emit rates
static var emitRateMin : float = 1.0;
static var emitRateMax : float = 2.0;

var enemyCube : GameObject;

function Start () {

while (true){

The “moveForward” script is attached to every enemy object, this script controls the speed the object moves at. It will eventually be updated to include health, armor and scale. All of which should be randomly generated. The moveForward script contains a variable called moveSpeed.

	var script : moveForward;

	yield WaitForSeconds(Random.Range(emitRateMin,emitRateMax));

	var newEnemy = Instantiate (enemyCube, transform.TransformPoint(Vector3(Random.Range(-9, 9), 0, 0)), Quaternion.Euler(0, Random.Range(80, 100), 0));

This line is causing the problem I think. Instead of modifying the moveSpeed variable of each instance, it modifies the variable of the original object only. This value keeps going up and up with each new object instantiated. The instances are unchanged.

		script.moveSpeed += Random.Range(-0.5,2);

	}

}

Full script without comments:

    static var emitRateMin : float = 1.0;
    static var emitRateMax : float = 2.0;

    var enemyCube : GameObject;

    function Start () {

	while (true){

		var script : moveForward;

		yield WaitForSeconds(Random.Range(emitRateMin,emitRateMax));

		var newEnemy = Instantiate (enemyCube, transform.TransformPoint(Vector3(Random.Range(-9, 9), 0, 0)), Quaternion.Euler(0, Random.Range(80, 100), 0));

    		script.moveSpeed += Random.Range(-0.5,2);
    
    	}
    
    }

Something like this:

var newEnemy = Instantiate (enemyCube, transform.TransformPoint(Vector3(Random.Range(-9, 9), 0, 0)), Quaternion.Euler(0, Random.Range(80, 100), 0));
script = newEnemy.GetComponent(moveForward);
script.moveSpeed += Random.Range(-0.5,2);

“This line is causing the problem I think. Instead of modifying the moveSpeed variable of each instance, it modifies the variable of the original object only. This value keeps going up and up with each new object instantiated. The instances are unchanged.”

Is it static? Static variables are class members not instance members. Meaning there is only one shared variable for the whole class, when you set the one variable it updates for anywhere that reads it. They are not like an instance member where the instance gets its own memory. Static means that it belongs to the class. It can cause all sorts of concurrency issues so I would just avoid it.

As a general rule you should rarely if ever use static. It’s mostly bad practice and code smell and there are very few actual legitimate uses for it.