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
1
Question by charliepup03 · May 19, 2020 at 07:18 PM · scripting problemscripting beginnerscore system

Scoreboard not updating

using System.Collections; using System.Collections.Generic; using System.Diagnostics; using UnityEngine; using UnityEngine.UI;

public class CountScore : MonoBehaviour {

 public Text Scoreboard;
 public GameObject ball;


 private int leftPaddleScore = 0;
 private int rightPaddleScore = 0;


 // Start is called before the first frame update
 void Start()
 {
     ball = GameObject.Find("Ball");
 }

 
 void OnTriggerEnter2D(Collider2D Ball)
 {
     if (Ball.gameObject.name == "leftGoal")
     {
         rightPaddleScore++;
     }

     if (Ball.gameObject.name == "rightGoal")
     {
         leftPaddleScore++;
     }

     Scoreboard.text = rightPaddleScore.ToString() + " - " + leftPaddleScore.ToString();

     print(rightPaddleScore + " , " + leftPaddleScore);
 }

}

Comment
Add comment · Show 1
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 emmanuelsegun2008 · May 19, 2020 at 07:24 PM 0
Share

try

public Text Scoreboard; public GameObject ball; private int leftPaddleScore = 0; private int rightPaddleScore = 0; // Start is called before the first frame update void Start() { ball = GameObject.Find("Ball"); }

void OnTriggerEnter2D(Collider2D Ball) { if (Ball.gameObject.name == "leftGoal") { rightPaddleScore++; } if (Ball.gameObject.name == "rightGoal") { leftPaddleScore++; } Scoreboard.text = rightPaddleScore.ToString() + " - " + leftPaddleScore.ToString(); print(rightPaddleScore + " , " + leftPaddleScore); } void Update() { OnTriggerEnter2D; }

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by tadadosi · May 19, 2020 at 08:45 PM

Start by adding a Debug.Log at the start of your OnTriggerEnter2D method to see if you are actually hitting something.


It could be something like:

 Debug.Log ("Found object: " + Ball.name); 
 // You should change Ball to the default name collision or something like hit
 // cuz it's not the Ball what you are receiving, it's the hit info of any object that 
 // the gameobject with your script collided with.


If there is no hit info, them you should check out why not, it could be that you are missing some configuration in your gameobjects, like having a collider with isTrigger disabled and missing a rigidbody2D component.

Comment
Add comment · Show 3 · 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 19, 2020 at 08:55 PM 0
Share

I definitely think its connecting, because every time the ball hits a goal I get this message:

0 , 0 UnityEngine.$$anonymous$$onoBehaviour:print(Object) CountScore:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/CountScore.cs:39)

avatar image tadadosi charliepup03 · May 19, 2020 at 09:14 PM 0
Share

I think your OnTriggerEnter2D code should be in a script for the actual Paddle that you want your ball to hit, each Paddle with its own script and they send a message to the scoreboard after they get a hit by the ball.


And you just simple check if what hits the paddle was the ball by adding a tag to the ball.

  void OnTriggerEnter2D(Collider2D collision)
  {
         if (collision.CompareTag("Ball"))
         {
             // score++;
         }
 }


avatar image boss2070301 charliepup03 · May 20, 2020 at 12:37 AM 0
Share

Did you check is trigger on your collider.

avatar image
1

Answer by emmanuelsegun2008 · May 19, 2020 at 07:24 PM

try

public Text Scoreboard; public GameObject ball; private int leftPaddleScore = 0; private int rightPaddleScore = 0; // Start is called before the first frame update void Start() { ball = GameObject.Find("Ball"); }

void OnTriggerEnter2D(Collider2D Ball) { if (Ball.gameObject.name == "leftGoal") { rightPaddleScore++; } if (Ball.gameObject.name == "rightGoal") { leftPaddleScore++; } Scoreboard.text = rightPaddleScore.ToString() + " - " + leftPaddleScore.ToString(); print(rightPaddleScore + " , " + leftPaddleScore); } void Update() { OnTriggerEnter2D; }

Comment
Add comment · Show 3 · 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 19, 2020 at 07:38 PM 0
Share

like this?

void OnTriggerEnter2D(Collider2D Ball) { if (Ball.gameObject.name == "leftGoal") { rightPaddleScore++; }

     if (Ball.gameObject.name == "rightGoal")
     {
         leftPaddleScore++;
     }

     Scoreboard.text = rightPaddleScore.ToString() + " - " + leftPaddleScore.ToString();

     print(rightPaddleScore + " , " + leftPaddleScore); 
 }
 
 void Update()
 {
     OnTriggerEnter2D;
 }

}

avatar image charliepup03 charliepup03 · May 19, 2020 at 07:43 PM 0
Share

I got this error when I made the changes

Assets\Scripts\CountScore.cs(44,9): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

avatar image emmanuelsegun2008 charliepup03 · May 19, 2020 at 08:29 PM 0
Share

then you might have to find a workaround I don't know anything else.

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

236 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

Related Questions

Can't increase my score after OnTriggerEnter occurs. 1 Answer

How to add different score when player hit different enemy,how to add different scores while player hitting different type of enemies 1 Answer

Standard Asset FP Controller not moving 0 Answers

Character not jumping high enough 1 Answer

,How to get the script of an element? or the GameObject of an Component? 1 Answer


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