- Home /
Javascript How to Activate/Deactivate GameObject Arrays
I am currently trying to activate an array of game objects that I have assigned in Unity. I have a very simple script that doesn't do all that I want it to do, however I'm hoping that with this I can learn how to work with arrays. All I want to do is activate the objects in this array.
pragma strict
ar values : GameObject[];
function Start () {
//I want to declare that it is active here
}
ExTheSea told everything. You cannot activate or deactivate the whole array with one command, you need to loop on all of them.
@ExTheSea, please convert your comment to an answer, so it can be marked as correct :)
Is your probelm now solved? If so please accept the answer to mark the question as solved.
Answer by ExTheSea · May 20, 2013 at 08:15 PM
you should just go through the Array with a for-loop and then activate the gameobjects in the array:
var values : GameObject[];
function Start () {
for(var i = 0;i<values.Length;i++){
values[i].SetActive(true);
}
}
You need to use SetActive(true) actually; the .active variable is deprecated.
Right, Eric5h5 although i think it's still working as i think i once tried it before i posted this answer but i could be mistaken.
It will still work at the moment, but it's a good idea to get in the habit of SetActive now because the old method won't keep working forever, and you don't want to have to go back and fix code.
Your so right. Removing deprecated methods out of your code as soon as soon as they get deprecated is a really good idea. I just didn't know yet that gameobject.active is deprecated as i converted my project just recently. I also edited my answer for future reference.
@ExTheSea It's only a good idea if you can get the same behaviour some other way, without using the depricated method. This (afaik) does not apply to Input.eat$$anonymous$$eyPressOnTextFieldFocus
. Even though it's depricated, there is no other way of getting the same behavious... :(