How to use a script on multiple gameobjects, then change a variable for one of them, not the other.

I made a respawn script, to respawn an enemy once it has died. Everything works so far, but I want to place respawn gameobjects multiple times in the level. Here's my script:

var beholder : Transform;
var targetThing : Transform;
var Weapon: Transform;
private var isClose : boolean = false;
Beholder.thing = targetThing;
Beholder.weapon = Weapon;
var deathTimer : int = 0;
var maxRespawns : int;

static var respawnCount : int = 0;
function Update () 

    if(Vector3.Distance(targetThing.position,transform.position) <= 30)
        isClose = true;
        isClose = false;
    if(Beholder.death && isClose)


        if(deathTimer == 50 && respawnCount < maxRespawns)
            deathTimer = 0;
            Beholder.death = false;
            var newBeholder = Instantiate(beholder, transform.position, transform.rotation);



By multiple objects, I meant that I wanted to make multiple versions of the object this script is attached to so that they can have different respawnCount variables. That way i could have different respawn places that have seperate numbers of possible enemies to respawn. Then when it adds one to respawnCount of one respawn object, it doesn't to the other.

What specifically are you trying to change? If you place multiple gameObjects in your level and each has this script, you can change the exposed variables for individual objects in the editor (just click on them and tweak the script values).

For changing values through code, you just need to reference the gameObject the script is attached to and the specific variable in the script. Take a look at Accessing Other Game Objects and Accessing Other Components from the scripting reference to get you started.

Your problem here is that:

static var respawnCount : int = 0;

respawnCount is a static variable, and therefore is a member of the class, not the instance. In order to make it exposed, it should look like this:

public var respawnCount : int = 0;

In doing this you will not be able to access it by MyClassName.respawnCount anymore, but you will be able to edit it in your inspector panel in the Editor. This way every object that this script is attached to can have a different value for that variable.

Hope that helps.


My problem was actually Beholder.death. I made a public variable and set that to equal Beholder.death in Update() Now it respawns the enemy gameobjects from both Respawn gameobjects at the same time like I wanted, but it does that really fast! I'll tell you guys if I figure it out, but it would help if you could think about it too.