Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Once script but all objects destroyed

Discussion in 'Scripting' started by tmanallen, Jan 2, 2016.

  1. tmanallen

    tmanallen

    Joined:
    Nov 8, 2009
    Posts:
    395
    Hopefully a simple a question but this has stumped because it hasn't happened until now, I have a script like this

    Code (csharp):
    1.  
    2. void OnCollisionEnter2D(Collision2D col){
    3.  if(col.gameobject == player.gameobject)
    4. {
    5.          Destroy(col.gameobject);
    6. }
    7.  
    8. }
    9.  
    10.  
    Now I have attached this a pickup item and when the player collides with the object all the objects with this script is being destroyed. How would I go about doing something like this without it destroying all game objects? I have done this before and each individual item would get collected as they are touched by the player but now all the objects are being destroyed.


    any suggestions?
     
  2. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    Hey, try it with name or tag :)
    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D col){
    2. if(col.gameobject.name == player.gameobject.name)
    3. {
    4.          Destroy(col.gameobject);
    5. }
    6. }
     
    tmanallen likes this.
  3. tmanallen

    tmanallen

    Joined:
    Nov 8, 2009
    Posts:
    395
    Ok let me try and see what happens.
     
  4. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    If that script is attached to your pickup objects, then wouldn't you actually be destroying your entire player with the Destroy(col.gameobject) call? Since the "col" object would be the player rather than the object itself.

    If I'm understanding that right, you really should change that to Destroy(gameobject) rather than col.gameobject.

    I'm not sure if that would fix your problem, because I can't see how your scripts interact, but just something that looks off.
     
  5. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    Depends on on which object the script is attached to ^^ but it looks like it's definitely not the player while looking at the IF question. But seriously, this guy is registered since 2009 year.. usually he should know those stuff.
     
  6. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    Yeah, but I was going off his OP and it sounded to me like he had it attached to the pickup object, but was destroying the player when it entered the collider.

    I was going off this line:

    Maybe I misunderstand the wording since the sentence is a little wonky though :p
     
  7. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    "all the objects with this script is being destroyed" Never heard something about that. That's impossible unless the objects are childs of the parent object. So i guess, he's talking about the player. Change the destory command to Destory(this.gameobject); and everything will be fine.
     
  8. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    Back to OP:

    This should fix things if your scripts are attached like they should be. Though, I don't think you even need the "this." since gameobject inherently references its own gameobject.
     
  9. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    Yeah you don't need to use this but it's more understandable then ^^
     
  10. tmanallen

    tmanallen

    Joined:
    Nov 8, 2009
    Posts:
    395
    Yeah I tried to the naming convention and it still didn't work, so I had to create a script for each individual gameobject, which is very strange.