Unity 5 - Brick.cs onCollissionEnter2D isn't being called

Running Unity 5.4.1.f1 personal.

I followed this lecture and ran into the problem that Application has been deprecated so I used the information in the [Application.Loadlevel obsolete](http://Application.Loadlevel obsolete) discussion and put in a print statement but on running it, I never get to that print statement located in onCollissionEnter2D in brick.cs.

I’m wondering if I did something wrong when I set up the prefab for the bricks . The brick script section looks OK. Any thoughts on what link I must have broken to prevent the function from being called? The brick.cs code is identical to the lesson code. It is only the levelmamager code that is different due to the deprecation issues.

Here is an image of the brick prefab settings for the 1 hit brick. Let me know what else you might need to see to make suggestions.

Thanks,
Steven

1 Like

can you post a copy of your brick.cs script please.

Just wondering as in your post you mention onCollissionEnter2D.

this is one of those in build MonoBehaviours that if spelled incorrectly will not generate an error during compilation or runtime, they simply will not get called correctly.
it should be in the format of ‘OnCollisionEnter2D’, since c# and therefore its respective callbacks are case sensitive.

ie
void OnCollisionEnter2D(Collision2D collision){

2 Likes

Good point Obo,
@Steven_Chabotte, if @OboShape fix don’t solve it (if you did used onCollisionEnter2D, it will probably solve), would you mind sending the code so we can take a look?

Both the brick and the ball have to have Collider2D components, with “Is Trigger” unchecked. And as mentioned, spelling has to be exact.

If using Visual Studio 2015, right click and near the bottom of the right-click menu is “Quick MonoBehaviours”. All the (not yet implemented) MonoBehaviours are listed, plus a count of how many are implemented. So if you implement Start, Update, and OnCollisionEnter2D, it will show “3”. If the latter is spelled wrong, it will not be counted, and you would be able to find it (spelled correctly) in the list.

MonoDevelop probably has some similar feature.

Code is below. I checked and I had the spelling correct. The ball has a Rigidbody 2D and a Circle Collider 2D with Is Trigger unchecked, The bricks have Box Collider 2D with Trigger unchecked.

using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour {

    public int maxHits;
    private int timesHit;
    private LevelManager levelManager;

    // Use this for initialization
    void Start () {
        timesHit = 0;
        levelManager = GameObject.FindObjectOfType<LevelManager>();
    }

    // Update is called once per frame
    void Update () {
    }

    void onCollisionEnter2D (Collision2D col) {
        timesHit++;
        print("in collider");
        SimulateWin();
    }

    // TODO remove this method once we can actually win
    void SimulateWin () {
        levelManager.LoadNextLevel();
    }

}

Try changing the lower case “o” in “onCollisionEnter2D” to uppercase, e.g. “OnCollisionEnter2D” - these methods are case-sensitive.

1 Like

Thank you. I totally missed that. Problem is now solved.

1 Like

You’re welcome :slight_smile:

Privacy & Terms