Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by raul corrales · Dec 06, 2010 at 04:09 AM · javascriptclasses

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

A node in a childnode? 1 Answer

Help me with this JavaScript class please(Script inside) 2 Answers

Getting Error "No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(UnityEngine.GameObject)' was found." 0 Answers

Cube terrain with perlin noise 1 Answer

Marching cubes in Unity? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges