Hey guys, For some reason my ball doesn’t trigger my Brick’s collision script, stopping the level from changing from 1 to 2.
Note* the prints on the collision do not appear in the console after the ball collides with the brick, and bounces off of the brick*
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 OnCollisonEnter2D (Collision2D col) {
print("!!!!");
timesHit++;
print("its a hit");
SimulateWin();
}
//TODO Remove this method once we can actually win!
void SimulateWin() {
levelManager.LoadNextLevel();
}
Are you using Unity 5? i have had the same issue, but am using unity 5. i have had to define a rigidbody2d in my script, to get the ball to bounce.
I am troubleshooting ho to get the hit to register. will post back when i find it.
No hits are registered in the variable timeHits as per seen in the inspector. The code doesn’t flow at that function resulting in not loading the next level.
This I’ve tried:
I’ve tried to check the code in deep detail (mispelling mistakes, missing comas, block letter, etc).
I’ve also unlicked the Bricks.cs from the bricks prefabs or anything that could be using the code. Then I reconnected the code to these assests by creating a new component. I other words, I repeated the procedure.
I’ve checked in forums and obviously I’ve compared Ben’s code to my. I’ve done this several time and they match perfectly. Perhaps I’m too immersed in the problem, so I can’t see the difference.
Here’s my code without a very fancy look sorry. It’s very readable though!
using UnityEngine;
using System.Collections;
public class Brick : MonoBehaviour {
private int maxHits;
public int timesHit;
private LevelManager levelManager;
// Use this for initialization
void Start () {
timesHit = 0;
levelManager = GameObject.FindObjectOfType <LevelManager>();
}
// Update is called once per frame
void OnCollisionEnter2d (Collision2D col) {
timesHit++;
SimulateWin();
}
//TODO Remove this method once we can actually win!
void SimulateWin () {
levelManager.LoadNextLevel();
}
The most probable result will be that you won’t read anything, cause I suspect that you attached a Box Collider to the brick game objects, instead of a Box Collider 2D.
If this doesn’t solve the problem, post some screenshots of the bricks and ball prefabs with all the components clearly visible.
Might be worth checking the IsTrigger options too, Ben took you through a section or two where these options were experimented with so something may have been left ticked/unticked unintentionally.
Updated Fri Jan 27 2017 17:32
Your method name is incorrect also, it should be OnCollisionEnter2D notice the capitalised “D”.
Thanks so much for your answers! All of them were extremely helpful.
I reckon we make this sort of mistakes when learning to code. Attention to the detail, is definitely a must in this arena.
Also, It’s a good habit to use Debug.log as it scopes mistakes for you.
Lastly but not least important. I’m getting into the community vibe and notice recently why communities can be very helpful. I want to be more active in this one. So, any help you need I’ll try to be helpful.