Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by charliepup03 · May 12, 2020 at 01:02 AM · scripting problemscript.gamescripting beginner

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!");
     }
     
 }
 
 



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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();
          }
      }
  }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image charliepup03 · May 12, 2020 at 01:52 AM 0
Share

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.

avatar image boss2070301 charliepup03 · May 12, 2020 at 03:06 AM 0
Share

Did you put

   public void EndGame(){
  Debug.Log("Game Over");
  }

before on collision?

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image charliepup03 · May 12, 2020 at 03:21 AM 0
Share

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 :/

avatar image ADiSiN charliepup03 · May 12, 2020 at 03:41 AM 0
Share

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.

avatar image
0

Answer by charliepup03 · May 12, 2020 at 03:55 AM

here's what I got


leftgoal.png (135.9 kB)
rightgoal.png (138.0 kB)
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ADiSiN · May 12, 2020 at 04:06 AM 0
Share

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");
  }


avatar image charliepup03 · May 12, 2020 at 04:11 AM 0
Share

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

avatar image ADiSiN charliepup03 · May 12, 2020 at 04:16 AM 0
Share

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");
avatar image charliepup03 ADiSiN · May 12, 2020 at 04:23 AM 0
Share

can you specify where the lower case "goal" is? I cant seem to find it.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

279 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges