- Home /
Destroying Instantiated Obect Problem
So I have this script that when I press t it spawns a wood plank in front of you that you can place down. However I can't get the board to go away when you press the same button. Any ideas on how I could do this? Here's the script:
if (Input.GetKeyUp("t") && woodAvailable > 0) {
if ( buildingMaterial == true) {
Destroy(obstacleToBuild);
buildingMaterial = false;
}
if (buildingMaterial == false){
playerPosition = transform.position;
playerRotation = transform.rotation;
obstacleToBuild = Instantiate(woodPlank, myGamobject.transform.FindChild("Main Camera").position + 3f * Vector3.forward, myGamobject.transform.FindChild("Main Camera").rotation) as GameObject;
obstacleToBuild.transform.parent = myGamobject.transform.FindChild("Main Camera");
buildingMaterial = true;
}
}
yeah it is. It instantiates fine and all that. I just cant get it to go away. i think its because after its instantiated it makes building$$anonymous$$aterial = true. Then when I press it again all in the same frame it goes through making it false again and then instantiating again. I need a work around for that.
Answer by whydoidoit · Jun 19, 2012 at 05:10 PM
if (Input.GetKeyUp("t") && woodAvailable > 0) {
if ( buildingMaterial == true) {
Destroy(obstacleToBuild);
buildingMaterial = false;
} else {
playerPosition = transform.position;
playerRotation = transform.rotation;
obstacleToBuild = Instantiate(woodPlank, myGamobject.transform.FindChild("Main Camera").position + 3f * Vector3.forward, myGamobject.transform.FindChild("Main Camera").rotation) as GameObject;
obstacleToBuild.transform.parent = myGamobject.transform.FindChild("Main Camera");
buildingMaterial = true;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613073012im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Destorying prefab and instanstiating again? 0 Answers
instantiate object 2 Answers
Removing instantiated objects?? 1 Answer
How do I destroy a Instantiated UI image that is pushed on the Canvas? 2 Answers
Distribute terrain in zones 3 Answers