- Home /
Question by
kittyninja · May 09, 2018 at 08:47 AM ·
instantiatedestroy
Destroy last placed object and put a new one?
So I want a waypoint movement, I place a waypoint and the player moves to there. But I don't know how to destroy the last waypoint and put a new one.
public transform destination;
if (Input.GetMouseButtonDown(1))
{
if (Physics.Raycast(ray, out hit, 200))
{
//Tried putting DestroyImmediate(destination,true) but it destroyed the one I was putting as well;
Instantiate(destination, hit.point, Quaternion.identity);
}
},So I want on mouse click to place a waypoint to which my player moves but I don't know how to delete it and place a new one so there is only 1 at all time.
public transform destination;
if (Input.GetMouseButtonDown(1))
{
Debug.Log("Pressed secondary button.");
if (Physics.Raycast(ray, out hit, 200))
{
Instantiate(destination, hit.point, Quaternion.identity);
}
}
Comment
Best Answer
Answer by Happeloy · May 09, 2018 at 08:51 AM
What you need to do is to store the new instantiated object, and then delete that.
GameObject _gameObject = Instantiate(destination, hit.point, Quaternion.identity);
Destroy(_gameObject);
Your answer
Follow this Question
Related Questions
Mouse Methods (OnMouseDown, OnMouseOver etc) stop working after certain circumstances 0 Answers
Extra objects keep appearing in scene view 0 Answers
Respawning gameobject 2 Answers
Cannot destroy instantiated objects 2 Answers
Instantiate problem 1 Answer