- Home /
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!
Answer by robertbu · Oct 19, 2013 at 09:06 PM
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.
That sorted my rotation issues, thanks. Although the destroying is still amiss, it's now throwing up a "Null reference" error when I press the valid key.
Could I also do:
cloneBubble.transform.position = player.transform.position + Vector3(0, 10, 0); //To spawn above the player?
Yes, you can do the line you describe, and I've edited my answer since that is what it should be. So what does your code look like now, and on what line is the null reference exception? If you make bubbleShield a GameObject ins$$anonymous$$d of a Transform does the problem go away?
$$anonymous$$y code, looks the same apart from I used this ins$$anonymous$$d of what I had:
var cloneBubble = Instantiate(bubbleshield) as GameObject;
cloneBubble.transform.position = player.transform.position + Vector3(0, 10, 0);
I tried changing the Transform to GameObject, but brought this up:
InvalidCastException: Cannot cast from source type to destination type.
As for the Null Reference, that is called when line 15 comes into play, so:
cloneBubble.transform.position = player.transform.position + Vector3(0, 10, 0);
In taking a close look I found a couple more problems:
You were declaring 'cloneBubble' with local scope, so it was not available in the Destory(). Also you have 'player' as a transform, but are calling 'player.transform.positon'. I think the following is what you want. I made a few other, not bug changes:
#pragma strict
var player : Transform;
var bubbleshield : GameObject;
private var cloneBubble : GameObject;
private var canSpawn : boolean = true;
private var timeUntilNextSpawn : float;
function Update ()
{
if(canSpawn && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.B))
{
cloneBubble = Instantiate(bubbleshield);
cloneBubble.transform.position = player.position + Vector3(0,10,0);
canSpawn = false;
timeUntilNextSpawn = 0.0;
}
if(!canSpawn)
{
timeUntilNextSpawn += Time.deltaTime;
}
if(timeUntilNextSpawn >= 5)
{
canSpawn = true;
Destroy(cloneBubble);
}
}
That looks more promising! Thanks for taking the time to look and help out. Ill try and run this script tomorrow as I'm not at the PC. I will be sure to vote up and mark as answered when done. All the best.