How do I use arrays in Unity?

I’m trying to make a game where my character can switch through various weapons while he’s running along. I’m not worrying about how many weapons he can hold or how he picks them up - just about switching between them. I have an array in a script that I assign the weapon objects to in the inspector. Here is the part of the script that causes the trouble.

var weapons : GameObject[ ];

var weaponNum = 0;
var weaponCount = 4;

private var gun : GameObject = Instantiate(weapons[weaponNum], transform.position, transform.rotation);
gun.transform.parent = this.transform;

function Update () {
if(Input.GetKeyDown(“tab”)) {
gun.transform.DetachChildren();
Destroy(gun);
weaponNum++;
if(weaponNum == weaponCount) {
weaponNum = 0;
}
var gun : GameObject = Instantiate(weapons[weaponNum], transform.position, transform.rotation);
gun.transform.parent = this.transform;
}

The rest of the code that I didn’t put up there is irrelevant to this problem.

When I run the code, I get this error:
System.NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices:NormalizeArrayIndex (System.Array array, Int32 index)

So, how do I set my object reference to an instance of an object? Thanks for any help that anyone can give.````

First, the code indented to make it easier to read :slight_smile:

var weapons : GameObject[]; 

var weaponNum = 0; 
var weaponCount = 4; 

private var gun : GameObject = Instantiate(weapons[weaponNum], transform.position, transform.rotation); 
gun.transform.parent = this.transform; 

function Update () { 
  if(Input.GetKeyDown("tab")) { 
    gun.transform.DetachChildren(); 
    Destroy(gun); 
    weaponNum++; 

    if(weaponNum == weaponCount) { 
       weaponNum = 0; 
    } 

    var gun : GameObject = Instantiate(weapons
       [weaponNum], transform.position,
       transform.rotation); 
    gun.transform.parent = this.transform; 
  }
... rest of Update()

Have you dragged your weapon prefabs in the inspector to the weapons array in the inspector? If not, there will be no objects in the array at start.

Yeah, I did that, but I think the problem is that it thinks that it is passing around pointers (references) to the weapons instead of using the actual objects. Also, sorry about the code. :roll:

Ok, now it seems to be mostly working with no change to that part of that script. Could be that there was some interference.

Now I’m working on changing in between the weapons again, and I noticed a problem. The first gun disappears and the next one appears fine. The next time I hit the “switch” button, the new gun appears, but the old one doesn’t go away. Any suggestions?
Admittedly, the first gun is not the same thing as the next ones, but the variable is named the same thing, so I don’t think it should matter.

Oh wait - I may have been struck by inspiration…

Ok - I was. I just make “gun” a child of the script and when I delete it, I don’t do it by name - I just delete all the kids and I’m done, and it doesn’t matter what gun I’m using. I can play!

Shouldn’t you put:

private var gun : GameObject = Instantiate(weapons[weaponNum], transform.position, transform.rotation); 
gun.transform.parent = this.transform;

In a Start() or Awake() ?

Well, I didn’t need to, but I did eventually do that just so the code is a little bit clearer. If you have code outside of functions, it should run at the beginning of the game anyways, just like a Start() or Awake() function would.

Hi there! I believe Start() and Awake() are slightly different in when they are being called in the game, but I guess in your example it isnt a problem

Writing something outside of a function is the same as writing it in the Start function. For clarity it is always better to write it in the Start function.

But in this particular case there is another problem.

This code:

private var gun : GameObject = Instantiate(weapons[weaponNum], transform.position, transform.rotation); 
gun.transform.parent = this.transform;

Is different from this code:

private var gun : GameObject:
gun = Instantiate(weapons[weaponNum], transform.position, transform.rotation); 
gun.transform.parent = this.transform;

You can not call Instantiate as part of a variable declaration. Javascript declares variables at initialization time which is different from the time the Start function is called. When initializing a float to a default value or a Vector3 to say Vector3.zero or Vector3 (1, 1, 1); this is not a problem, but if you are accessing unity runtime functionality it won’t work.

Unity 1.2.2 now gives an error messages when you call such a function from a variable declaration.