• 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 /
  • Help Room /
avatar image
0
Question by GamesForTheWeird · Dec 02, 2021 at 03:15 PM · scripting beginner

How do I make my score system work?

I am trying to add a point to the scoreboard everytime the ball hits a trigger but it will not run properly and I am unsure why. Help please? I'm new to unity btw.,I trying to add 1 to the scoreboard which is a text but it won't let me. Im new to coding btw.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ScoreManager : MonoBehaviour
 {
     private void OnTriggerEnter2D(Collider2D collision)
     {
         Debug.Log("Trigger!");
         GameObject scoreBoard = GameObject.FindWithTag("Score");
         score = scoreBoard.GetComponent<Text>();
         int point = 1;
         score = score + 1;
 
 
     }
 }



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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dsiak · Dec 03, 2021 at 02:27 AM

You got a few things mixed up. Lets take a look at the variables. 1. As peer your code score inst being probably set because it has no type (int, string, float) 2. scoreBoard.GetComponent() is doing exactly that, getting the COMPONENT text, and not what is written in the component, what you are getting is the component you see in the inspector with all those options like Bold font size alignment etc. Therefore score is not a string but a Text object 3. Even if it was a string you cant sum a number integer to a number that is written (a string) without first converting the string to a number. Lets try to fix what you have but I want you to try and implement some improvements after.

 public class ScoreManager : MonoBehaviour
 {
     private void OnTriggerEnter2D(Collider2D collision)
     {
         Debug.Log("Trigger!");
         GameObject scoreBoard = GameObject.FindWithTag("Score");
         //A variable type string
         //scoreBoard.GetComponent<Text>() gets the Text component, the .text grabs what is written in the Text component
         string score = scoreBoard.GetComponent<Text>().text;
         int point = 1;
         //Parses the string score to an integer and sums to point
         int scoreInt = int.Parse(score) + point;
         //Parses the integer scoreInt to string
         score = scoreInt.ToString();
     }
 }

That should work, but it aint based. Try the following to improve the code

  1. Make a integer variable "scoreTotal" and have it store the player's score, that way you dont have to parse the string every time you need to increase the score, instead you just sum to scoreTotal and write that to score

  2. Check the tag of the collision so you can have different types of ball give different amounts of score

  3. Create a variable "Text score" at the start of the script and have it setup to the component only once, that way you avoid making a double search (Right now you find the scoreBoard and them you grab its component, you can just assign the component to a variable from the get go). This many searches will slow down your game if 100 balls hit the player in a sort amount of time.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

183 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

Related Questions

I don't know why my script wont work 1 Answer

Why does the 'Edit Script' button not open any external editor? 2 Answers

2D Fighter AI 0 Answers

How to make a FreeLook camera 1 Answer

Script not working for me 2 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