Question

I can do in unitycript as an object "enemy" that has "health", "speed" and "stamina". And to delete the object, or several at once?

class enemy {
    health = 100
    speed = 10
    stamina = 200
}

for 0 to 10
{
    enemyBig = new Enemy ()
}


if keydown (space)
{
    delete all.enemyBig 
}

How would this code in unityscript correctly?

Was it helpful?

Solution

To keep track of all your enemies at once, what you need is called an array. There's a good tutorial there:

OTHER TIPS

In javascript, to define a class you create a function.

function Enemy {
   this.health = 100;
   ....
};

and then to put methods on the prototype (because javascript uses prototypal inheritance.)

Enemy.prototype.theMethod = function () { ... };

when you do the above to define a method, the method is an instance method; i.e. every object has its own copy of the method. If you want to define a 'static' method, you just put the method on the class

Enemy.staticMethod = function() {...};

the difference is for the former you can do

var enemy1 = new Enemy();
enemy1.theMethod(); // this in the theMethod refers to enemy1

and for the latter you do

Enemy.staticMethod(); // there is only one staticMethod for the entire class.

To implement an object:

function Enemy {
    this.health = 100;
    ...
};

Enemy.prototype.attack = function() {
    this.health -= 10;
    ...
};

var boogerMonster = new Enemy();
boogerMonster.attack();

As far as deleting an object, the garbage collector will take care of the object if it has no other references to it.

But, from the question, it looks like just knowing this information won't carry you far. Grab a book on JavaScript and/or do some online research to really understand the fundamentals of JavaScript.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top