- Home /
Help with class in Javascript
Hi to all, sorry for my bad english...
I created a code to put chairs in a room:
Code in Javascript:
class chair {
function chair()
{
this.positionx=0;
this.positiony=0;
this.positionz=0;
}
static var positionx:int;
static var positiony:int;
static var positionz:int;
}
function Start()
{
for(var i : int = 0; i < 5; i++)
{
room = new chair();
chair.positionx=0;
chair.positiony=0;
chair.positionz=Random.Range(-10, 10);
}
}
function Update ()
{
}
In this code, I created a class (chair) to create and place chairs at positions X, Y, Z that I choose. With the loop "for", I add five chairs in the room and I put the Z position of the chairs is random between (-10 and 10).
My questions are:
---how do I move all the chairs at once?
---how do I delete a chair? and all?
I tested this code in Update loop
chair.entity.transform.position.x=chair.entity.transform.position.x = +1;
But it only moves the chair that was created last
A greeting and thanks
Answer by pyro · Dec 06, 2010 at 04:38 AM
That's because you have the position variables as static, those are shared between all instances of your chair class.
A better way of doing what you are trying to do is to ignore creating your own class in Javascript (unless you have a special reason to, but in general..) since Unity automatically inherits from MonoBehavior, just create a chair.js script file. Then you can use parenting to create a container GameObject for all of your chairs to move them at once, and if you destroy that container it will destroy all the child chairs. Plus you can Add and Remove the script files as components.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Help me with this JavaScript class please(Script inside) 2 Answers
Cube terrain with perlin noise 1 Answer
Marching cubes in Unity? 1 Answer