x


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;
    }
    else
    {
        isClose = false;
    }
    if(Beholder.death && isClose)
    {

        deathTimer++;

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

}

Edit:

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.

more ▼

asked May 24 '10 at 09:11 PM

oz m gravatar image

oz m
126 14 17 25

btw, Beholder is the script for the enemy.

May 24 '10 at 09:13 PM oz m
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered May 24 '10 at 09:27 PM

straydogstrut gravatar image

straydogstrut
1.2k 29 38 60

I mean that i want to make multiple respawn objects with seperate respawnCount variables.

May 24 '10 at 09:29 PM oz m

Simply removing the 'static' keyword will make it a local variable then, only having effect in this script. Is it necessary to make respawnCount a global variable? Are you accessing it from anywhere else?

May 24 '10 at 09:50 PM straydogstrut

ya but i think i can manage to not have to, thanks!

May 24 '10 at 09:51 PM oz m

it isn't working

May 24 '10 at 09:55 PM oz m
(comments are locked)
10|3000 characters needed characters left

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.

==

more ▼

answered May 24 '10 at 09:48 PM

equalsequals gravatar image

equalsequals
4.5k 16 28 64

Hi ==, I haven't used 'public' myself, can you explain the difference between using that and using var alone? The only reference I can find to it is in the Unify Keywords list. By saying it can be accessed by other classes, does this mean it's accessible from other scripts (as opposed to using 'static')? Sorry, I still need to learn many JS fundamentals. Thanks.

May 24 '10 at 10:00 PM straydogstrut

@straydogstrut that is a good question and should probably be a new thread / question.

May 24 '10 at 10:21 PM Maltus

@Maltus Sure thing, will do=)

May 24 '10 at 10:38 PM straydogstrut

@equalsequals I just wanted to let you know that up until this point I didn't realize the difference b/t static and public. You just made my day a LOT easier!

Jul 28 '10 at 08:20 PM Joshua Falkner
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 24 '10 at 10:04 PM

oz m gravatar image

oz m
126 14 17 25

I got it! Thanks for all your help guys!

May 24 '10 at 10:08 PM oz m

No worries, sorry I was a little busy to really help out. The best problems are the ones you solve yourself though, well done!

May 24 '10 at 10:39 PM straydogstrut
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3329
x342
x291
x184
x110

asked: May 24 '10 at 09:11 PM

Seen: 1867 times

Last Updated: May 24 '10 at 09:39 PM