- Home /
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.
This 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.
This is my script. it's simplified.
rs is RegSaver.js, usedObj is Boolean[], isExploded is boolean (used when an object exploded), and RegisterThis Function added in function Update if the object has exploded (if(isExplode) RegisterThis)
function RegisterThis(){
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 this script
I'm sorry, I really don't understand the question. Can you reword it? What, exactly, do you want this function to do?
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)
'one value', 'all the same value'- to me, these are talking about the same thing.
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 think that you are messing up some terminology: when you talk about "arrays", you are thinking about "array's elements", don't you? Because you just have a single array, that's usedObj.
Then, I think 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 RegisterThis(){
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.
Yes, you can. I was not sure about that possibility in UnityScript.
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)
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.
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? Something like this...
function RegisterThis(var i){
if(isExploded && rs.usedObj[i] == true){
rs.usedObj[i] = isExploded;
}
}
-Larry
Your answer
Follow this Question
Related Questions
assigning arrays with for in 1 Answer
How to save an array to PlayerPrefs? 1 Answer
How to Get a PlayerPrefsX boolean? 1 Answer
bool array to playerprefs 1 Answer
Saving Array Objects in Android. 0 Answers