- Home /
Access specific GameObject in arrays
(Insert greeting here) :)
I am some what new to Unity started two months back and I recently started learning about arrays and I now have a general understanding of said thing.
I did a search here on UA on how to access a GameObject in a array, but as I said I am pretty new so I do not really understand all of it so I decided to ask my own question.
I do not know how to access and use a gameObject in a array what I have tried is like so:
var weapons : GameObject[]; // Array holding all the weapons
function Update ()
{
weapons.m16.SetActiveRecursively(false); // m16 is the name of the weapons that is going to be stored in the array.
}
But it says m16 is not a member of weapons. And of course it is not I know that, but I do not know what else to do.
So my question is: How do I get access to a object in a GameObject array and use it.
Thank you. :)
Answer by Eric5h5 · Jul 04, 2011 at 04:17 PM
The same way you access anything in an array, by the index.
weapons[0].SetActiveRecursively(false);
Hi, Eric. Thank you for your reply but, it says something weird: BCE0048: Type `UnityEngine.GameObject´ dose not support slicing.
I have never seen that error and as I am new I have seen my fair share of errors.
Can you help? :)
Ha, I feel so stupid. Yes I did not define it as an array. Thank you.