Logging Objects, Increasing a Variable's Value

I am trying to create a structure in which the player destroys an object (representing picking it up) and then instantiates it elsewhere (representing putting it down.

This is the code I have to pick up the object which destroys the object perfectly well:

var acorn : GameObject;

function OnTriggerStay(acorn : Collider) {
	if (Input.GetKey("1")  acorn.collider)
		Destroy(acorn.gameObject);
}

Now I need to add to this script to prevent a player from picking up another object until he has put down the first object.

I had thought the best way to do this would be to create a variable called acornCount with an initial value of 0. But when the player picks up an object, acornCount is increased by 1.

So I tried this, but it has made the player not able to pick up any objects at all:

var acorn : GameObject;
var acornCount = 0;

function OnTriggerStay(acorn : Collider) {
	if (Input.GetKey("1")  acorn.collider  acornCount < 1)
		Destroy(acorn.gameObject);
		acornCount++;
}

The script works when I remove “acornCount++;” so I assume the problem lies with that line.

I used the ammoCount variable from the FPS tutorial pt. 2 and i can’t see what I’ve done differently to mess it up.

Can anyone tell me how to make this work?

	if (Input.GetKey("1")  acorn.collider  acornCount < 1)
		Destroy(acorn.gameObject);
		acornCount++;

This needs to be:

	if (Input.GetKey("1")  acorn.collider  acornCount < 1) {
		Destroy(acorn.gameObject);
		acornCount++;
	}

Otherwise only the first line after the If statement is actually part of the If statement, and the acornCount++ executes every frame during OnTriggerStay (which would quickly make it a very large value…). This is why I always use braces even just for one line…the shortcut of leaving them out if you have only one lines saves a bit of typing, yeah, but it makes things inconsistent and easy to mess up like that if you decide to add more lines later. :wink:

–Eric

Also, you may want to use a boolean variable instead of an integer.