- Home /
Can you destroy an object using GetInstanceID?
I have two scripts. One is on the monster and one is my game manager. When the player hits the monster I want to save its ID in my game manager so I can destroy it a bit later.
Is is possible to do something like Destroy(instance id)?
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "Player")
{
gameManager.encounterId = GetInstanceID();
}
}
Thank you!
no, as I am aware of. The workaround may be found here: https://stackoverflow.com/questions/44017154/how-to-access-gameobject-by-instance-id-in-unity
basically, using GetinstanceID to name an object and then Find by the name string.
Why do you want to do this? Why can't you just set a reference to the object and Destroy it the normal way?
game$$anonymous$$anager.encounterObject = this.gameObject;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Destroy by Tag isnt working,Can't Destroy By Specific Tags 2 Answers
Destroy Objects after a set time 2 Answers
transferring variables between scripts c# 3 Answers
how to destroy enemy 1 Answer