I’m a noob when it comes to unity and I cant figure out how to get the collision to work for my goals when the ball hits them. I used a debug.log to see if the collision works and named it (“GAME OVER”), but it wont show up. Again I really have no idea what i’m doing, but I’m learning.
CODE FOR BALLCOLLISION
using UnityEngine;
public class BallCollision : MonoBehaviour
{
public Ball movement; // reference to Ball script
public GameManager gameManager;
void OnCollisionEnter (Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Goal")
{
movement.enabled = false;
FindObjectOfType<GameManager>().EndGame();
}
}
}
CODE FOR GAMEMANAGER:
using UnityEngine;
public class GameManager : MonoBehaviour
{
public void EndGame()
{
Debug.Log("GAME OVER!");
}
}
what you can do is to see if it is actually calling it is to move the EndGame into the ball collision script like so
using UnityEngine;
public class BallCollision : MonoBehaviour
{
public Ball movement; // reference to Ball script
public GameManager gameManager;
public void EndGame(){
Debug.Log("Game Over");
}
void OnCollisionEnter (Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Goal")
{
movement.enabled = false;
EndGame();
}
}
}
Hi!
Can you confirm these:
- Both interacted objects has colliders;
- The ball GameObject has Rigidbody component.
In order for OnCollisionEnter to be detected one of the collider should have Rigidbody attached to it.
Could you also check and Debug your functions this way:
void OnCollisionEnter(Collision collisionInfo)
{
Debug.Log("Collision detected, but yet without correct tag");
if (collisionInfo.collider.tag == "Goal")
Debug.Log("Collision detected with correct tag");
}
Will you see the output in console?
Both of them?
Also, there is no need to call EndGame by FindObjectOfType since you have created reference to GameManager in your script already. You can simply call it this way:
gameManager.EndGame();
Let me know if it helps you.