Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 sam2k13 · Aug 15, 2014 at 08:40 AM · guiscorescoreboard

Scoreboard will not update

I am brand new to unity and trying to make a simple scoreboard. When a collision occurs, however the scoreboard does not change. Please help. Here is part of the code:

 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour {
     public GameObject enemy;
     public GameObject laser;
     public GUIText gamerscore;
     float spawnTimer;
     float shootTimer;
     int score;
 
     // Use this for initialization
     void Start () {
         Instantiate (gamerscore);
         spawnTimer = 1.0f;
         score = 0;
         updatescore ();
     }
     public void addscore(int addnum){
         
         score += addnum;
         updatescore ();
     }
     
     void updatescore(){
         
         gamerscore.text = score.ToString();
     }

and

 public class EnemyController : MonoBehaviour {
     public float speed;
 
     public GameObject explosion;
     public int score;
     GameController gameController;
     // Use this for initialization
     void Start () {
 
         GameObject gameref = GameObject.FindWithTag ("MainCamera");
         if (gameref != null) {
                         gameController = gameref.GetComponent<GameController> ();
                 }
         if (gameref == null) {
             Debug.Log("GameController not found");
                 }
                  
     }
     
     void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.tag.Equals("Laser"))
         {
             Destroy(other.gameObject);
             Destroy(this.gameObject);
             Instantiate(explosion, this.transform.position, this.transform.rotation);
             gameController.addscore(score);
 
         }
     }



Comment
Add comment · Show 7
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 The-Little-Guy · Aug 15, 2014 at 12:40 PM 0
Share

Does at least one of the objects have a Rigidbody attached to it?

avatar image Landern · Aug 15, 2014 at 01:03 PM 0
Share

@Ekta-$$anonymous$$ehta-D score by default as declared in the GameController class would be private and not accessible unless the access level was explicitly changed.

@sam2k13, try putting a Debug.Log statement in the if statement before the game controller call to see if OnCollisionEnter is invoked at all.

avatar image sam2k13 · Aug 15, 2014 at 05:28 PM 0
Share

I did some debugging and the method add score is being called and score is increasing by 10. The issue is when the updatescore function is called from the addscore function it is not changing the guitext on the screen to the new string. I have noticed that sometimes after I exit out of game mode then the text will change.

avatar image nathanielstevens · Aug 15, 2014 at 08:33 PM 0
Share

Why are you instantiating GamerScore?

avatar image sam2k13 · Aug 15, 2014 at 08:38 PM 0
Share

to display the GUIText on the screen

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by alebasco · Aug 15, 2014 at 09:15 PM

The problem is that the gamerscore references the prefab, not the newly created gameobject.

When you write

 Instantiate (gamerscore);

You are creating a new copy of that prefab, but you did not store that copy anywhere, so you do not have a reference.

Later, when updatescore() is called,

 void updatescore()
 {
     gamerscore.text = score.ToString();
 }

you try to change a value on the prefab, not the object you created.

Instead, you will need two variables

 public GUIText gamerScorePrefab;
 private GUIText gamerScore;

When you instantiate the object, instead write

 GameObject newGamerScore = Instantiate(gamerScorePrefab.gameObject) as GameObject;
 gamerScore = newGamerScore.GetComponent<GUIText>();

Then when you update the score,

 gamerScore.text = score.ToString();

you are now referencing the object you created, instead of the prefab.

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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

GUI Text Score counter 3 Answers

Help! Why doesn't this work? GUI/Static vars Help! Javascript 1 Answer

Coin Counting Problem 0 Answers

How to Use Leader Board Third Party API ? 2 Answers

Calculating player distance meter? C# Unity2D 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