1

First: I´m totally new with programming and this must be an easy task so I hope you can help me to fix this while I learn from your approximation to this so maybe I can solve future problems in my beginner project.

So I´m trying to make a gameobject to change its sprite when it collides with another one. In the code you can see that I made the CandleLit and CandleUnlit with public properties so I dragged the corresponding sprites to their respective slot in the Inspector in Unity... the idea is that when the CandleUnlit collider touches the collider of my object with tag "Smallfire" it switch the sprite of CandleUnlit to the sprite of CandleLit... I don´t have errors in console but nothing is happening when the collision occurs so I know it must be a very stupid problem whit the way I understand the scripting flow... so I hope someone can help me to find what i´m missing or what did I do wrong. Thanks in advance I will check my tutorials while waiting for someone´s help because I can´t figure it out after many hours by my self :(

public class CandleSpriteSwitch : MonoBehaviour
{
    public Sprite CandleLit;
    public Sprite CandleUnlit;

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "SmallFire")
        {
            gameObject.GetComponent<SpriteRenderer>().sprite = CandleLit;
        }
        else
        {
            gameObject.GetComponent<SpriteRenderer>().sprite = CandleUnlit;
        }
    }
}
Rob Acuña
  • 35
  • 8
  • Did you try putting a breakpoint in `OnCollisionEnter` to see if you are ever entering it? Are you using a 2D collider? If so, try using [`OnCollisionEnter2D`](https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html) instead. – Alex Myers Apr 06 '19 at 05:10
  • Thanks for your reply. Yes I´m using 2D colliders but when I add the 2D, the whole "OnCollisionEnter2D" changes color so I thought that would´t work as intended... So tell me Alex that change of color doesn´t matters in this case? Or i am missing something to make the program interpret that part as intended? – Rob Acuña Apr 06 '19 at 05:29
  • And thank you for your time I really appreciate it :) I don´t get what you mean with the break point part by the way :( Sorry for that – Rob Acuña Apr 06 '19 at 05:31
  • You also have to convert the `Collision` parameter to `Collision2D`. The method signature is different. – Alex Myers Apr 06 '19 at 05:34
  • Ok You where right of course. Now it is interpreted as it should be, everything is on 2D now as you suggested but it still doesn´t work :( do you have any more Ideas of what could be happening? – Rob Acuña Apr 06 '19 at 05:45
  • 1
    Add some break points and let us know if the code ever enters the if/else flows inside the OnCollisionEnter2D. – Alex Myers Apr 06 '19 at 05:47
  • The code for changing the sprite itself seems fine, the most likely problem is the collision isn't registering. Add a Debug.Log() call to the beginning of the OnCollisionEnter2D function. That way, a message will show up in your console when you actually get a collision. If you don't see any messages, there's a good chance that your collision isn't happening at all. If that's the case, I recommend looking into your collision layers, or making sure that both objects have relevant 2d colliders. – VPellen Apr 06 '19 at 12:59
  • Thanks for your words I'm checking how the breaking points work, I see those will be very helpful in the future...and for this problem to of course. – Rob Acuña Apr 08 '19 at 01:49

2 Answers2

1

One of your GameObject needs to have a BoxCollider2D and a Rigidbody2D. Collisions do not take place until and unless a rigidbody2d is present. So in your case just make sure the 2 GameObjects that are colliding have a Rigidbody2D and a Collider2D

Zohaib Zaidi
  • 288
  • 4
  • 11
  • Yes both have 2Dcolliders and 2Drigidbody, the problem was that "is trigger" was checkedBut I had it that way hoping to register that one collider was over the other without actually crashing because my Character is holding the candle so the candle gets stuck in the collider while my character passes through. I see that is not the case now, but is there a way to make it possible or I have to find another solution? Thanks for taking the time to help I really appreciate it!!! – Rob Acuña Apr 08 '19 at 02:06
1

As @Zohaib Zaidi mentioned in collisions there must be at least 1 Rigidbody in order to work properly. If you have rigidbody and sizes of 2 colliders are adjusted correctly and are not set to "is Trigger". Then the only reason could be in Layer Collision Matrix, if that's fine, then there is no reason for it to not work!

public class CandleSpriteSwitch : MonoBehaviour
{
    public Sprite candleLit;
    public Sprite candleUnlit;
    public SpriteRenderer render;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.transform.CompareTag("SmallFire"))
        {
            render.sprite = candleLit;
        }
        else
        {
            render.sprite = candleUnlit;
        }
    }
}

Btw.. when declaring a name variable always use a lowercase for the first word. In general this shouldn't be a problem but it will for sure create difficulties later on. Plus, you could actually declare Sprite Renderer on the beginning and not when collision occurs.

Nikola G.
  • 335
  • 1
  • 3
  • 9
  • Thank you Nikola!!! I will use lowercase for the first word from now on... I'm new in the programers world and I really appreciate those tips. And yes you where totally right, the problem was the "is trigger" checked... But I had it that way hoping to register that one collider was over the other without actually crashing because my Character is holding the candle so the candle gets stuck in the collider while my character passes through. I see that is not the case now, but is there a way to make it possible or I have to find another solution? – Rob Acuña Apr 08 '19 at 02:03
  • And of course thanks for your time and help I am very interested in learning but I guess that learn by my self was way harder than I thought. I appreciate your help A LOT!!! – Rob Acuña Apr 08 '19 at 02:09
  • @RobAcuña For that you could use different Layers and adjust their detection in Layer Collision Matrix. Read about Layers [here](https://docs.unity3d.com/Manual/class-TagManager.html), and [here](https://docs.unity3d.com/Manual/LayerBasedCollision.html) about Layer Collision Matrix. – Nikola G. Apr 08 '19 at 08:17
  • 1
    I can't believe I missed the info in the second link... it is so much info to digest quickly I guess. Thank you very very much for your help man!!! – Rob Acuña Apr 08 '19 at 09:06