• 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
Question by sandhillceltic · Jan 31, 2014 at 02:18 AM · javascripterrorfor-looptroubleshootingjavascript-specific

For statement errors?

So, I am working on a parenting script that will make an array of objects parent to another object. Before I go any further, here is my script-

 var allforces : GameObject [];
 var editmode = true;
 
 
 function Update () {
     allforces = GameObject.FindGameObjectsWithTag("force");
     
     
     if(Input.GetKeyDown("space")){
         editmode = !editmode;
         
         if(editmode == true) {         //what happens if the player switches from edit mode to play mode
             Gotoedit ();
         }
     }
 }
 
 
 
 function Gotoedit () {
     for(var i : int = 0; !i = allforces.length; i++) {
         allforces[i].transform.parent = GameObject.FindGameObjectWithTag("fuse");
     }
 }


The first part of the script works perfectly (function Update), but the (function Gotoedit) does not work. I keep getting the errors

  1. Assets/Standard Assets/Scripts/modemanager.js(21,56): BCE0043: Unexpected token: ).

  2. Assets/Standard Assets/Scripts/modemanager.js(21,33): BCE0044: expecting ;, found '='.

This is very annoying, as I cannot work on the script (or the game, for that matter) until I have fixed the errors. This is my first time messing around with FOR statements, so pardon me if this is an noob mistake. I got all my references from the following links-

http://unity3d.com/learn/tutorials/modules/beginner/scripting/loops

http://answers.unity3d.com/questions/11547/how-do-i-use-for-to-create-loops-in-my-script-and.html

I seem to have gotten the form of the FOR statement right, but I remain getting the same result. Could someone PLEASE figure out what I'm doing wrong?

*Javascript only, please

*Thanks

Comment

People who like this

0 Show 0
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

Answer by ffxz7ff · Jan 31, 2014 at 02:27 AM

You probably want

 !i == allforces.length

instead of

 !i = allforces.length

== is a comparison, = is an assignment.

Comment
Bunny83
sandhillceltic

People who like this

2 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 Bunny83 · Jan 31, 2014 at 02:35 AM 0
Share

He probably want to use

     i < allforces.length

instead since it's shorter, the most common for statement condition and more safe than checking for inequality.

btw inequality has it's own operator:

     i != allforces.length
avatar image ffxz7ff · Jan 31, 2014 at 02:37 AM 0
Share

Yeah, that would be less weird indeed.

avatar image sandhillceltic · Jan 31, 2014 at 02:47 AM 0
Share

ffxz thanks a lot! I actually knew that, I must have missed it. Thanks!

avatar image sandhillceltic · Jan 31, 2014 at 02:49 AM 0
Share

I changed it to

 function Gotoedit () {
     for(var i : int = 0; !i < allforces.length; i++) {
         allforces[i].transform.parent = GameObject.FindGameObjectWithTag("fuse");
     }
 }


as that seems to suit my purpose better. Thanks for the speedy reply, I needed that tonight!

avatar image ffxz7ff · Jan 31, 2014 at 05:38 AM 0
Share

You're welcome! Please accept the answer if it helped.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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 avatar image

Related Questions

For statement stops function? 1 Answer

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

For loop not working? 1 Answer

Help solving for loop error 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