2
\$\begingroup\$

So I have 2 GameObjects, and they have exactly the same script attached to them called 'RoomSpawner'. Via this script, I want to destroy just ONE of these TWO GameObjects. How can I do this? By typing Destroy(gameObject); It will destroy 2 GameObjects :/, I'm confused, any ideas?

Those 2 GameObjects are in the same positions, and I want to delete them by OnTriggerEnter2D

\$\endgroup\$
  • 1
    \$\begingroup\$ Ok, but which one do you want to destroy? If you can explain to me how to make that distinction, then you can write that distinction into code. \$\endgroup\$ – Draco18s Sep 4 '18 at 20:29
0
\$\begingroup\$

Pretty simple. Have the one you don't wanna destroy with a public bool and check it true in the inspector:

//other variables...
public bool doNotDestroy;
//other code...
function OnTriggerEnter(Collider collision)
{
  //other statments...
  if (collision.gameObject.name = /*name of needed object to hit*/  && doNotDestroy == false)
{
  //destroy code...
}
}
//rest of code follows...

Comments indicate places where the rest of your script would be.

\$\endgroup\$
  • 1
    \$\begingroup\$ Yea, that would work.. BUT due to my project (procedural dungeon generator) I have to randomly delete ONE of the 2 GameObjects, no matter which one. Im coonfused, I've tried whole night :D \$\endgroup\$ – Seweryn Pokutycki Jul 4 '18 at 10:05
  • \$\begingroup\$ @SewerynPokutycki Please add the fact that it's random to your question \$\endgroup\$ – clabe45 Aug 4 '18 at 2:21
  • \$\begingroup\$ @SewerynPokutycki yes that would be helpful to others: to edit in the fact these objects are generated run-time \$\endgroup\$ – The Mattbat999 Aug 4 '18 at 4:33
0
\$\begingroup\$

I can put my head through, ok Create An empty gameObject and make your GameObjects Childrens of the former, Add the script and create a private List of gameObjects, Set it in the Start,
Use foreach statement to get the component GameObject,
Instead of Destroy, SetActive the gameObject at the index to False. I think that will do.

\$\endgroup\$
  • 1
    \$\begingroup\$ This doesn't appear to answer the question of how to destroy one and only one of the objects via OnTriggerEnter2D. \$\endgroup\$ – DMGregory Dec 4 '18 at 5:06
0
\$\begingroup\$

Super simple solution use static variables

\$\endgroup\$
  • 1
    \$\begingroup\$ Exactly how would you recommend applying static variables to solve this problem? \$\endgroup\$ – DMGregory Dec 4 '18 at 12:48
-2
\$\begingroup\$

Okay So I found out solution, I've simple checked GameObject's ID and destroy them.

\$\endgroup\$
  • \$\begingroup\$ Thanks for providing the answer! Don't forget to come back in a couple of days to mark it as the accepted one! \$\endgroup\$ – Vaillancourt Jul 4 '18 at 11:46
  • 1
    \$\begingroup\$ @Seweryn can you elaborate on how you did this? \$\endgroup\$ – clabe45 Aug 4 '18 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.