- Home /
Change all value of variable
I want to change all the values of a variable to false(in this case) Where I have something like this:
Element0 : true
Element1 : true
Element2 : true
Element3 : false
Element4 : true
........
and there you keep going, so for this I would like to know how to set all of this to false, something like this:
example[0-50] = false;
I already know another way to do it but I don't want something so complex that is this:
for(var i = 0;i < 50; i++){
example[i] = false;
}
But as I say I want something more easy like the first example.
Note: I want it to work also with other kind of variables like integers.
Answer by Bunny83 · Apr 18, 2011 at 01:51 AM
Well, if you have seperate variables, they have nothing to do with each other so you can't set them all to something with one (short) command. If it's a collection (array or enumerable type) the easiest way is what you wrote, using a for loop.
Some examples:
var example = new int[5]; // 5 integer variables in one array
for (var i in example) { i = 0; }
or the index approach like you did:
var example = new int[5]; // 5 integer variables in one array
for (var i = 0; i < example.length; i++) { example[i] = 0; }
Yep. And they aren't that "complex". Use them a bit and you'll find them easy enough to use.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Access and Change variables from other Scripts? 2 Answers
Countdown code not working 1 Answer
On Button press Move Value toward 2 Answers