- Home /
Question by
ReasoningReason · Dec 12, 2016 at 08:09 AM ·
destroy object
Destroying An Instance of an object and childs
Hey guys, I have been trying to work out how to Destroy MiniMenuBase after Instantiating it with right click and then being able to create it again if I right click again for a few hours now and can't seem to get it to work. I think it is a issue with how I am Instantiating it but I do not have the experience to see what the issue is. Thank you
public GameObject MiniMenuBasePrefab;
public GameObject MoveButtonPrefab;
public GameObject AttackButtonPrefab;
public GameObject DefendButtonPrefab;
private bool IsMiniMenuOpen;
void Awake()
{
Instance = this;
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
CheckMouseInput();
}
//
private void CheckMouseInput()
{
GameObject MiniMenuBase = null;
if (Input.GetMouseButtonDown(1) && OverWatchVault.VltSelectedUnitIsHoveredOver == true && OverWatchVault.VltSelectedUnit != null && IsMiniMenuOpen != true)
{
IsMiniMenuOpen = true;
CreateMiniMenu(MiniMenuBase);
}
if(Input.GetMouseButtonDown(0) && IsMiniMenuOpen == true && MiniMenuBase != null)
{
DestroyMiniMenu(MiniMenuBase);
}
}
//
private void CreateMiniMenu(GameObject _MiniMenuBase)
{
_MiniMenuBase = Instantiate(MiniMenuBasePrefab);
_MiniMenuBase.transform.position = OverWatchVault.VltSelectedUnit.transform.position;
_MiniMenuBase.transform.rotation = OverWatchVault.VltSelectedUnit.transform.rotation;
Canvas MiniMenuBaseCanvas = _MiniMenuBase.GetComponentInChildren<Canvas>();
CheckAllUnitInteractions(MiniMenuBaseCanvas);
}
//
private void DestroyMiniMenu(GameObject _MiniMenuBase)
{
DestroyObject(_MiniMenuBase);
}
Comment
Answer by sisse008 · Dec 12, 2016 at 09:05 AM
bool first_right_click = true;
void Update(){
if ((Input.GetMouseButtonDown(1))
{
if (first_right_click)){
MiniMenuBasePrefab.SetActive(false);
first_right_click = false;
}
else {
MiniMenuBasePrefab.SetActive(true);
}
}
}
Your answer
Follow this Question
Related Questions
Javascript. Respawning a character after destroyobject(). 0 Answers
destroy prefabs 1 Answer
Help with C# - destroy at a certain position not working 0 Answers
Destroy(gameObject) not working 2 Answers
2d edge collider wont destroy? 0 Answers