• 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 COLLAnitySV · Jan 13, 2012 at 01:37 PM · arrayplayerprefsbooleanfor-loopplayerprefsx

For In Loop Fills all values in arrays problem

Return from my past questions... I have a problem with assigning values on unassigned array values. When i'm trying to make for in loops, the script assigned same values on all arrays. Now my question is, how to make the script just assign one values in an array and then it will assign another values later.

T$$anonymous$$s script i use for assigning 'used' values on an arrays, so if one objects exploded it will assign just one 'true' on one array, not all arrays.

T$$anonymous$$s is my script. it's simplified.

rs is RegSaver.js, usedObj is Boolean[], isExploded is boolean (used when an object exploded), and RegisterT$$anonymous$$s Function added in function Update if the object has exploded (if(isExplode) RegisterT$$anonymous$$s)

 function RegisterT$$anonymous$$s(){
     for(var i : int = 0; i < rs.usedObj.length; i++){
         if(isExploded && rs.usedObj[i] == true){
             rs.usedObj[i] = isExploded;
         }
     }
 }

I hope it's not too hard. Thanks for the answer. i really need t$$anonymous$$s script

alt text

Comment

People who like this

0 Show 3
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 syclamoth · Jan 13, 2012 at 01:44 PM 0
Share

I'm sorry, I really don't understand the question. Can you reword it? What, exactly, do you want this function to do?

avatar image COLLAnitySV · Jan 13, 2012 at 01:52 PM 0
Share

oh i'm sorry... what i want to do is assigning just one value in an array (i had a problem, it's assigning all same value in an array)

avatar image syclamoth · Jan 13, 2012 at 02:40 PM 0
Share

'one value', 'all the same value'- to me, these are talking about the same thing.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by BiG · Jan 13, 2012 at 02:16 PM

I'm not sure at all yet on what you are trying to implement, COLLAnitySV, but I would try to give you an answer. Sorry if it will miss your intentions.

"When i'm trying to make for in loops, the script assigned same values on all arrays"

First of all, I t$$anonymous$$nk that you are messing up some terminology: when you talk about "arrays", you are t$$anonymous$$nking about "array's elements", don't you? Because you just have a single array, that's usedObj.

Then, I t$$anonymous$$nk that you want enable these array's elements one at a time, in an incremental way. In that case, you have to hack "i" variable to directly skip the remaining iterations of the loop:

function RegisterT$$anonymous$$s(){
    for(var i : int = 0; i < rs.usedObj.length; i++){
       if(isExploded && rs.usedObj[i] == true){
         rs.usedObj[i] = isExploded;
         i = (rs.usedObj.lenght) + 1;
       }
    }
}

I repeat, I've many doubts about the desired behaviour of your script. Sorry if I miss that.

Comment
COLLAnitySV
gamberetto
Lo0NuhtiK

People who like this

3 Show 4 · 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 syclamoth · Jan 13, 2012 at 02:40 PM 0
Share

Can't you use 'break' to break out of a for-loop?

avatar image BiG · Jan 13, 2012 at 02:45 PM 0
Share

Yes, you can. I was not sure about that possibility in UnityScript.

avatar image COLLAnitySV · Jan 13, 2012 at 02:59 PM 0
Share

Thanks! It's really work!!! but i got this message : NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)

avatar image BiG · Jan 13, 2012 at 03:53 PM 0
Share

I've just done a short research about your error, but, unfortunately, I can't help you (if that's an iPhone project, maybe you could look here: http://forum.unity3d.com/threads/69998-Strange-Array-problem, but that is an old post, and I'm not sure that it could help you so much)...

However, the fact that my solution works, it's a confirmation of the fact that your code is, in some manners, a lil' messy! Because my solution isn't "elegant" at all! __

As an advice, think on the possibility to implement @Larry Dietz's solution, under this one (+1 to him, BTW!) If you can do it, I think that your last problem will be gone.

Good luck, and let us know if you have other troubles.

avatar image

Answer by Larry-Dietz · Jan 13, 2012 at 02:16 PM

I may be misunderstanding the question, but if you only want 1 element set to true, why use a loop to loop through all elements of the array? Why not just pass the index that you want set? Somet$$anonymous$$ng like t$$anonymous$$s...

 function RegisterT$$anonymous$$s(var i){
     if(isExploded && rs.usedObj[i] == true){
        rs.usedObj[i] = isExploded;
     }
 }

-Larry

Comment
BiG
COLLAnitySV
gamberetto
Hellium

People who like this

2 Show 0 · 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.

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

7 People are following this question.

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

Related Questions

assigning arrays with for in 1 Answer

PlayerPrefsX plugin error when build for windows store 3 Answers

Attempting to store an Inventory Array in PlayerPrefsX 1 Answer

Why is this giving me an error? (ToBuiltin problems) 1 Answer

How to save an array to PlayerPrefs? 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