Instantiating and destroying object

Hello,

I’m wondering if someone can point me in the right direction of how to destroy my instantiated object, I looked at some other posts and this seemed to solve others but not for me. Maybe I’m doing something wrong.

#pragma strict

var player : Transform;
var bubbleshield : Transform;

private var canSpawn : boolean = true;

var timeUntilNextSpawn : float;

function Update () 
{
	if(canSpawn == true && Input.GetKeyDown("b"))
	{
		var cloneBubble = Instantiate (bubbleshield, player.transform.position + Vector3(0, 10, 0), Quaternion.identity);
		canSpawn = false;
	}
	
	if(canSpawn == false)
	{
		timeUntilNextSpawn += Time.deltaTime;
	}
	
	if(timeUntilNextSpawn >= 5)
	{
		canSpawn = true;
		Destroy(cloneBubble);
	}
}

As a side, my instantiated prefab spawns on it’s side as if the rotation is off. Yet when I drag the prefab into the scene, it’s fine. Not really sure if I can resolve that issue.

Cheers!

I’m not sure of your issue since you did not describe your problem, but typically I do my Instantiate() this way:

var cloneBubble = Instantiate (bubbleshield, player.transform.position + Vector3(0, 10, 0), Quaternion.identity) as GameObject;

‘as GameObject’ specifies the type so that cloneBubble get the right type. Also you are not setting timeUntilNextSpawn to 0.0 when you instantiate a new game object. This means that timeUntilNextSpawn will always be grater than 5.0 after the first time this code is run, which means that your object wil lbe destroyed right after it is created.

As for the rotation issue, I suspect that your prefab has a rotation other than (0,0,0). Your Instantiate() is forcing a rotation of (0,0,0) when you use ‘Quaternion.identity’. You can handle it like this:

var cloneBubble = Instantiate(bubbleshield) as GameObject;
cloneBubble.transform.position = player.transform.position + Vector3(0, 10, 0);

This will preserve the rotation of the prefab but set the position.