• 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 patrickbaumi · Apr 20, 2019 at 12:50 PM · score

Score doesnt display propery

Hey so I am pretty new to unity and just started working on my first game, however the score just doesn´t want to display properly. Heres the script on my score text:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class score : MonoBehaviour
 {
 Text scoree;
 public GameObject mario;
 public int scorevalue=0;
     void Start()
     {
          scoree = GetComponent<Text>();   
     }
 
 
     void Update()
     {
      scoree.text="Score:"+scorevalue;
     }
 }
 

and here´s how i change the score: (not the whole script)

 void OnTriggerEnter2D(Collider2D col){
     if (col.gameObject.tag.Equals("bullet")){
        Instantiate(blood,transform.position,Quaternion.identity);
         Destroy(Marioo);
   scoretext.GetComponent<score>().scorevalue+=1;
   Debug.Log(  scoretext.GetComponent<score>().scorevalue);
     }
 }

in the console it tells me the correct score (via Debug.Log(), but it displays the same score all the time and only updates it after i restart i want it to update properly and reset after death, does anyone have an idea how to fix this?

Thanks in advance!

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 surfuay · Apr 20, 2019 at 07:00 PM

i think you're getting a little too complex

don't bother in the update doing scoree.text = "score" + scorevalue; it's overriding your updated text everytime you collide with an object.

Instead you'll want 3 things. 1. you'll need to create a singleton of your score script...SUPER EASY HERE'S HOW

   //you are declaring your script and then giving it a handle doesn't matter if the 2nd word is the same name or whatever the junk you want
   public static score score;

now you're going to make sure (since this object is going to be available for communication) that there will only ever be one of it so you don't throw any crazy problems or errors

private void Awake() {

     if (score == null)
     {   
         score = this;
     }
     
     else if (score!= this)
     {    
         Destroy(gameObject);
     }
    
 }

  1. a PUBLIC method IN YOUR SCORE CLASS called

    public void UpdateScore() (or whatever the heck you want to call it. { //order is important, putting the score+=1 as the 2nd will update your score then add to it which means it will always be 1 behind. score+=1; scoree.text = "score" + score; }

23 and last, you'll need to communicate to your score script which is why we made the score script both public and static. THIS WILL TAKE THE PLACE OF YOUR GETCOMPONENT WHICH IS MY FAV WAY TO CROSS COMMUNICATE

  //INSTEAD OF THIS LINE
    scoretext.GetComponent<score>().scorevalue+=1;
    

REPLACE IT WITH THIS LINE

  //the script you're accessing.the handle name you gave the script.the method inside the script you're accessing.
  score.score.UpdateScore();
Comment
Add comment · Show 1 · 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 patrickbaumi · Apr 21, 2019 at 01:26 PM 0
Share

thanks for the answer but it tells me thate the type or namespace name "score" could not be find in the line:

 public static score score;

Also should the UpdateScore function just be score+=1?

avatar image
1

Answer by Isaac_Marovitz · Apr 21, 2019 at 02:28 PM

  1. Try to avoid using GetComponent, it slows down you project, instead drag in your objects in the Insepctor.

  2. It should be if (col.gameObject.tag == "bullet") not if (col.gameObject.tag.Equals("bullet"))

  3. The start and update method aren't needed

  4. Don't use the same text object for the word score and the number score doing score.text = "Score:" + score every frame can use up memory and can be slow

Instead just write this:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
 
  public class Score : MonoBehaviour {
 
     public Text scoreText;
     public int scoreNumber;
     public GameObject Mario; 
 
      void OnTriggerEnter2D (Collider2D col) {
      if (col.gameObject.tag == "bullet") {
      Instantiate(blood, transform.position, Quaternion.identity);
      Destroy(Mario);
      scoreNumber++;
      scoreText = scoreNumber.ToString();
      Debug.Log(“Score: “ + scoreText);
      }
  }
 


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
avatar image
0

Answer by noeyedeer321 · Oct 02, 2019 at 09:56 PM

Just make a public Text UI and drag your Text into there.

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

107 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

Related Questions

Score is added then immediately switched back 1 Answer

How do i add points when the object past a specific coordinate 1 Answer

Overwritting score with GUI 1 Answer

C#: Q about creating Score system using GUItext 1 Answer

Trying to make simple trigger score counter, having troubles. 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