- Home /
Deleteing a Spawned Object
I have been working on a mounting code and it works great now the only issue that is left is the fact that every time I mount I create a new mount and the old one is never deleted. How do I am really new to this spawning Idea and don't understand the code fully yet could some one help me with how to delete it in my if(isMounted == true) area here is the code.
using UnityEngine;
using System.Collections;
public class GUIandSpawn : MonoBehaviour
{
private string button1Text;
public GUISkin Location1Button;
public Transform thisChar;
public Transform hoverBoard;
public GameObject prefabToSpawn;
public Transform spawn;
public bool isMounted = false;
public int dist = 1;
void OnGUI()
{
GUI.skin = Location1Button;
if(isMounted == true)
{
if (GUI.Button(new Rect(350, 100, 65, 65),button1Text))
{
thisChar.GetComponent<ThirdPersonController>().enabled = true;
thisChar.GetComponent<ThirdPersonCamera>().enabled = true;
thisChar.transform.parent = null;
isMounted = false;
}
}
if(isMounted == false)
{
if (GUI.Button(new Rect(350, 100, 65, 65),button1Text))
{
Vector3 v3Pos = thisChar.position - transform.up * dist;
GameObject clone = Instantiate(prefabToSpawn,spawn.position, spawn.rotation)as GameObject;
thisChar.transform.parent = clone.transform;
thisChar.GetComponent<ThirdPersonController>().enabled = false;
thisChar.GetComponent<ThirdPersonCamera>().enabled = false;
clone.GetComponent<HoverBoards>().enabled = true;
isMounted = true;
}
}
}
}
Well i don't quite understand your question and why you made your code so that you get models just "lying around" but to answer your question. You can delete an object using
Destroy(gameobjectvariable);
If you already have a mount, why don't you just move it rather than Instantiate() a new one and delete the old one.
What I am trying to do is make it so that when you press the GUI button it spawns your mount under you, you can use it then press the same button or another button and get rid of it till you call it again by pressing the button. But I am not having much luck at this.
You could just enable and disable the mount and not bother with Instantiate() and Destory(). But if you want to destroy it, you have to save a reference to it:
At the top of the file:
private GameObject clone;
The Instantiate();
clone = Instantiate(prefabToSpawn,spawn.position, spawn.rotation)as GameObject;
And the Destroy:
Destroy(clone);
Convert this to an answer and I will mark it as correct as it was very helpful and helped me get what I needed.
Answer by Tomer-Barkan · May 21, 2013 at 09:04 PM
You should save reference to clone in your class, instead of making it a local object. Then to delete it simply call Destroy(clone)
. So your code can look like this:
public class GUIandSpawn : MonoBehaviour
{
private string button1Text;
public GUISkin Location1Button;
public Transform thisChar;
public Transform hoverBoard;
public GameObject prefabToSpawn;
public Transform spawn;
public bool isMounted = false;
public int dist = 1;
private GameObject clone = null;
void OnGUI()
{
GUI.skin = Location1Button;
if(isMounted == true)
{
if (GUI.Button(new Rect(350, 100, 65, 65),button1Text))
{
thisChar.GetComponent<ThirdPersonController>().enabled = true;
thisChar.GetComponent<ThirdPersonCamera>().enabled = true;
thisChar.transform.parent = null;
isMounted = false;
if (clone != null)
Destroy(clone);
clone = null;
}
}
if(isMounted == false)
{
if (GUI.Button(new Rect(350, 100, 65, 65),button1Text))
{
Vector3 v3Pos = thisChar.position - transform.up * dist;
clone = Instantiate(prefabToSpawn,spawn.position, spawn.rotation)as GameObject;
thisChar.transform.parent = clone.transform;
thisChar.GetComponent<ThirdPersonController>().enabled = false;
thisChar.GetComponent<ThirdPersonCamera>().enabled = false;
clone.GetComponent<HoverBoards>().enabled = true;
isMounted = true;
}
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Fading To New Scene Problem? 1 Answer
I know C#, but what Unity methods should I learn? 2 Answers
UDP implementation in Unity, unable to send to two different machines 0 Answers