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 sandhillceltic · Feb 01, 2014 at 05:12 PM · javascriptfor-looptroubleshootingjavascript-specificfor

For loop not working?

I am trying to make a script that will add and edit hinge joints to every object in the scene with the tag "force". I have rewritten this script multiple times, and now it has stopped working completely. Here is the code-

 var forces : GameObject[];
 function Update () {
     forces = GameObject.FindGameObjectsWithTag("force");
     if(Input.GetKeyDown("l")) {
         Gotoedit ();
     }
 }
 
 function Gotoedit () {
     for(var i = 0; !i == forces.length; i++){
         GameObject.FindGameObjectWithTag("fuse").AddComponent(Rigidbody);
         forces[i].AddComponent(Rigidbody);
         forces[i].AddComponent(HingeJoint);
         forces[i].GetComponent(HingeJoint).connectedBody = GameObject.FindGameObjectWithTag("fuse").rigidbody;
         forces[i].GetComponent(HingeJoint).spring.damper = Mathf.Infinity;
     }
 }



I moved all the forces[i] statements to the function Update (), and them it worked fine with one GameObject, so I know it't not the AddComponent/GetComponent. I tried adding a debug in the Gotoedit, so I know that it is being called when I press the L key. This leaves the FOR statement, which i'm assuming is the problem. I'm fairly new to FOR statements (not javascript), so I came here to get advice. Does anyone have ideas?

*Javascript only, duh

*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

Answer by StormSabotage · Feb 01, 2014 at 05:21 PM

 for(var i = 0; !i == forces.length; i++){

my god...

 for(var i = 0; i <= forces.length; i++){

take and please learn Javascript and "for" cycle syntax

Comment
Add comment · Show 5 · 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
avatar image StormSabotage · Feb 01, 2014 at 05:24 PM 0
Share

btw if you want to know when "A" not equals "B" you should write this:

 if(A != B)

not

 if(!A == B)
avatar image sandhillceltic · Feb 01, 2014 at 05:32 PM 0
Share

Thanks, I haven't taken classes on Unity, I've been $$anonymous$$ching myself in my spare time. Thanks!

avatar image sandhillceltic · Feb 01, 2014 at 05:33 PM 0
Share

Works perfectly!

avatar image StormSabotage · Feb 01, 2014 at 05:33 PM 0
Share
  GameObject.FindGameObjectWithTag("fuse").AddComponent(Rigidbody);

that's wrong string in your cycle, it will try to add Rigidbody to same GameObject every turn... do that on Start() function.

avatar image StormSabotage · Feb 01, 2014 at 05:37 PM 0
Share
 forces = GameObject.FindGameObjectsWithTag("force");

If your "forces" GameObjects always same you no need to re-assing variable every frame on Update(), move it to Start()

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

For statement stops function? 1 Answer

For statement errors? 1 Answer

Unity says that my script isn't doing anything? 1 Answer

Finding nearest object with a certain tag without for loops. 1 Answer

Collision of Character collider not working? 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