If Statement, Instantiate one Object only?

Im Stumped on how to instantiate only One object? If anyone could point me in the right direction or help out that would be awesome!

static var enemyCount : int = 0;
static var roomEntered : int = 0;

var itemDrop : GameObject;

function Update () 
{
	if(enemyCount < 1  roomEntered == 2)
	{
		print("you are in room 2");
		//how do I instantiate only one object?	
		Instantiate(itemDrop, Vector3(-15.7, -5.9, 31.3), Quaternion.identity);	
	}
}

The If statement is running inside the update function so that I can always check which room Im in and to know when all of the enemies in that room are gone. Then once the enemies are destroyed I would like to instantiate one object.

This is all working good except that it will instantiate 100’s of the itemDrop and Im stumped as to how to only instantiate one object. Any suggestions? Im thinking of trying a for loop but I think I’ll get the same results?

try something like this:

static var enemyCount : int = 0;
static var roomEntered : int = 0;

var itemDrop : GameObject;
var itemDropped = false;

function Update () 
{
	if(enemyCount < 1  roomEntered == 2 )
	{
		print("you are in room 2");
		
		if (itemDropped == false) {
			SendMessage("Enter");
		}	
	}
}

function Enter () {
	itemDropped = true;
	Instantiate(itemDrop, Vector3(-15.7, -5.9, 31.3), Quaternion.identity);
}

Wow it works :smile: Thank You Very, Very, Very, much sir :wink:

But how come it works?

if item dropped = false which it does, go to the Enter function then the Enter function makes it true why would it not just skip the Instantiate and go back out?

So it goes through the whole Enter function once and by making it now true it wont go back through the if statement. thats just awesome.

I got it now greatly appreciated :slight_smile:

ok, your welcome

Having ran into a similar problem and was fortunate to find this thread, I still find myself confused on why exactly the send Message needs to be used in this case and why the if statement stated before doesn’t work as intended.

Found this thread via a search, thought I’d share something.

Use your Game Manager script to detect how many of something is in the game world with :

int numberofItems = GameObject.FindGameObjectsWithTag(“Items”).Length;

Then use an if statement in another gameobject to check number of items held by the Game Manager before instantiating.
if (GameScript.numberofItems <= 1)
{
Instantiate(yadda yadda, yadda yadda, yadda yadda yadda);
}