2 quick questions about GameObject[] SOLVED

Hi all,

I can't find reference to GameObject[] in any manual, so I'm asking my most basic questions here. I've made a script for customising hats, and have made each hat a value of `var hats : GameObject[]` . Here's the script:

var hats : GameObject[];
var activeHat : int;

function Update () 
{
    if (PlayerPrefs.GetInt("ChosenHat") > /*I'm not sure here*/)
    {
        PlayerPrefs.SetInt("ChosenHat", 0);
    }
    hats[activeHat].SetActiveRecursively(true);
    hats[!activeHat].SetActiveRecursively(false);
}

My first question is: How do I access the 'size' part of the hats variable (i.e the max number of hats) and the second arises from the `hats[!active].SetActiveRecursively(false);` line. This works, however only when hats[activeHat] is 0 or 1. My 0 hat is just an empty game object, so the character has no hat, and the 1 hat is a cap. Whenever I flick the activehat variable between 0 and 1, it deactivates the cap and (I assume) activates the empty game object, and vice versa. However, when I make activeHat 2 or more, it activates the hats fine, but just doesn't deactivate any. So my second question is How do you get that working?

Thanks a bunch!

The size of the array is `hats.Length`. I don't know what you're trying to do with "!activeHat"; the ! means not, which only works with true or false (1 or 0). You definitely don't want to use PlayerPrefs every frame in Update. It's for storing info on disk, so only call PlayerPrefs once when necessary.