- Home /
Pong goal collision
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!");
}
}
Answer by boss2070301 · May 12, 2020 at 01:41 AM
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();
}
}
}
Assets\Scripts\BallCollision.cs(17,13): error CS0103: The name 'EndGame' does not exist in the current context
This is a little embarrassing, but I looked at multiple youtube videos on making pong in unity to make this so this might be why.
Did you put
public void EndGame(){
Debug.Log("Game Over");
}
before on collision?
Answer by ADiSiN · May 12, 2020 at 02:31 AM
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.
i made sure they had colliders and rigidbody. I put your debug code in and changed my Game manager script in. Still nothing in the console :/
I've just tested it out and it should work.
Create 2 cubes in your scene.
$$anonymous$$ake sure they both have Box Collider enabled.
Place Rigidbody on 1 of them and make sure Is Kinematic is not enabled.
On the second cube place script with OnCollisionEnter (or you can place on the one with Rigidbody, it doesn't matter).
Play scene and drag one cube into another via Inspector.
You should see Console debugging.
If you won't see Console debug then could you attach picture with your scene view how you are doing it, because it seems like you missing something.
Answer by charliepup03 · May 12, 2020 at 03:55 AM
here's what I got
Thank you, that clarified the issue.
Since you are working in 2D space you need to call OnCollisionEnter2D.
Try code below.
void OnCollisionEnter2D(Collision2D collisionInfo)
{
Debug.Log("Collision detected, but yet without correct tag");
if (collisionInfo.collider.tag == "Goal")
Debug.Log("Collision detected with correct tag");
}
I got this when the ball hit the wall
Collision detected, but yet without correct tag UnityEngine.Debug:Log(Object) BallCollision:OnCollisionEnter2D(Collision2D) (at Assets/Scripts/BallCollision.cs:14)
but nothing on the goal
Great, that means it's working, but the collider don't have right tag "Goal".
Keep in $$anonymous$$d that program$$anonymous$$ is case sensitive what means that "goal" not equal "Goal".
Also, for perfomance it is better to use CompareTag ins$$anonymous$$d of simple .tag, so you can change this:
if (collisionInfo.collider.tag == "Goal")
Debug.Log("Collision detected with correct tag");
to this:
if (collisionInfo.collider.CompareTag("Goal"))
Debug.Log("Collision detected with correct tag");
can you specify where the lower case "goal" is? I cant seem to find it.
Your answer
Follow this Question
Related Questions
(PLEASE HELP) hi guys! i have a problem with my Score text 1 Answer
GetComponent won't work 1 Answer
How to double spirte/gameobject/prefab and control the result on those items? 0 Answers
Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer
how to allow the key to only open 1 door rather than all of them? 0 Answers