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.
  2. Dismiss Notice

Differentiating Objects Upon Collision

Discussion in '2D' started by AreebM, Jun 18, 2014.

  1. AreebM

    AreebM

    Joined:
    Mar 27, 2014
    Posts:
    32
    I have a few objects of different color in the scene of my game.

    I can fire a bullet at these objects and destroy them based on some simple OnCollisionEnter code, but I want the objects to be destroyed only if the color of the bullet matches the color of the object.

    Any tips on how this could be accomplished?
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    How many colors are we talking about? Generally speaking, I'd say the easiest thing to do would be to tag the objects appropriately, and check the tag. Alternatively, you can set a public variable in their scripts, if you're already using the tag for something else.
     
    AreebM likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    You can access scripts and components on the other object from a collision, so one way to do it:
    Code (csharp):
    1.  
    2. public Color myColor = Color.blue;
    3. void OnCollisionEnter2D(Collision2D collision) {
    4. ColorTarget ct = collision.gameObject.GetComponent<ColorTarget>();
    5. if (ct && ct.color == myColor) {
    6. //do something
    7. }
    8. }
    9.  
    (This code assumes that on the other collider, there is a script called ColorTarget that has that property)

    It may be a good idea to use an enum to decide colors or something like that, as Pyrian was getting at; using the Color class in this way would mean that setting the color of the target slightly wrongly would result in failure. That is, a bullet of color (255,255,255) would not destroy a target of color (255,255,254), even though they look identical.
     
    AreebM likes this.
  4. Kilrath81

    Kilrath81

    Joined:
    Nov 19, 2013
    Posts:
    153
    cant you just tag the objects by color then check the tags on collision and match to the bullet?

    Tag the object with Red...

    then in a script ont he bullet

    Code (JavaScript):
    1.  
    2. public var bulletColor : String = "Red";
    3.  
    4. function OnCollisionEnter2D(col : Collision2D)
    5. {
    6.     if (col.gameObject.tag ==  bulletColor)
    7.     {
    8.         Destroy(col.gameObject);
    9.     }
    10. }
     
    Last edited: Jun 19, 2014
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    You can do that, but then, tags are terrible. Mainly because you can't make an object have more than one tag. So being in the habit of using tags is a bad habit to be in. It'll work fine for the first 3 or 4 times, but then you'll want to use tags on some object and you'll discover that the thing you need a tag on already has a tag for some other purpose. In this case, a month down the line, AreebM might discover that he now needs to determine whether or not a given object is a game piece or a part of the environment (I dunno what your game is like). He goes to use tags there but, oh crap, they are already tagged with their colors.

    Use MonoBehaviours instead, and store any information you need to on them. It'll sustain you through much more complicated games.
     
  6. AreebM

    AreebM

    Joined:
    Mar 27, 2014
    Posts:
    32
    I was already able to make it work with tags based on Pyrian's answer, but I will also look at MonoBehaviours. Thing is, I'm fairly new to game development so my first game is purposefully simple and using tags shouldn't be a problem. Thank you StarManta!
     
  7. AreebM

    AreebM

    Joined:
    Mar 27, 2014
    Posts:
    32
    I'm using CSharp and am learning programming as I build the game so I'm not familiar with JavaScript. However, I used this code to check the tag of the object my bullet is colliding with. This may not be ideal but it works for now.

    Code (CSharp):
    1. void OnCollisionEnter2D (Collision2D other) {
    2.  
    3. if (other.gameObject.CompareTag("Blue")) {
    4.       //Does something
    5.       }
    6. }
     
    Last edited: Jun 19, 2014
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    The reason to have the color's name as a variable is so that it shows up in the inspector. On each of your bullet prefabs, you can then simply type Red, Green, Blue, etc into the inspector, rather than having to write a new script. With "Blue" in quotes in the script the way you have it, that script is hardcoded to only ever work for blue bullets.

    Generally, if you can write a script that can be used for all things of one type (e.g. all your bullets), it's a better approach than hardcoding it for one thing of the type.
     
  9. AreebM

    AreebM

    Joined:
    Mar 27, 2014
    Posts:
    32
    I understand what you mean, having that issue currently but I've cheated with a workaround. Thanks for the tips StarManta