• 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 Jackop222 · Aug 05, 2015 at 04:36 PM · updatescore

Updating score

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Score : MonoBehaviour {
     private int score;
     private int UpdateScore;
     Text text;
 
     // Use this for initialization
     void Start () {
         text = GetComponent<Text>();
         score = 0;
     }
     public void  ISCORE(){
         
         UpdateScore ++;
         
         Debug.Log("U SCORED!");
     }
     
     // Update is called once per frame
     void Update () {
         score = UpdateScore;
     }
     void OnGUI(){
         GUI.Box(new Rect (0,0,100,50), "Score: " + score);
         GUI.Box(new Rect (Screen.width - 100,Screen.height - 100,100,50), "Version 0.01 ");
     }
 
 }


Why does this not update to show the score, the "ISCORE" is called by another script. when it is called i want the score to increase by 1. any help?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by GeorgesAbitbol · Aug 05, 2015 at 06:52 PM

I dont use c# but I my guess would be this has something to do with variable scope, as ISCORE is a public but UpdateScore is a private ?

Do you get the debug message when you score ?

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 Major0 · Aug 05, 2015 at 07:30 PM 0
Share

He's manipulating the two integers by calling ISCORE which is a public method. I tried his code, by calling ISCORE when mouse left button is up and it worked perfectly. No idea why he is experiencing this issue.

avatar image imilanspinka · Aug 05, 2015 at 08:50 PM 0
Share

This is more like a comment than an answer..

avatar image Jackop222 · Aug 06, 2015 at 09:13 AM 0
Share

Yes i am getting the debug message!

avatar image
0

Answer by Major0 · Aug 05, 2015 at 08:51 PM

EDIT ToString() is automatically called on score since it's being concatenated with a string...

I just tried your script with a little modification, and it worked O.o anything else you're missing? Make sure you're actually calling it from other scripts...

Here's the working version of your script (ISCORE is triggered with right mouse click down).

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class TestScript : MonoBehaviour
 {
     private int score;
     private int UpdateScore;
     Text text;
 
     // Use this for initialization
     void Start()
     {
         text = GetComponent<Text>();
         score = 0;
     }
     public void ISCORE()
     {
 
         UpdateScore++;
 
         Debug.Log("U SCORED!");
     }
 
     // Update is called once per frame
     void Update()
     {
         score = UpdateScore;
         if (Input.GetMouseButtonDown(0))
             ISCORE();
     }
     void OnGUI()
     {
         GUI.Box(new Rect(0, 0, 100, 50), "Score: " + score);
         GUI.Box(new Rect(Screen.width - 100, Screen.height - 100, 100, 50), "Version 0.01 ");
     }
 }

Couple notes:

  • If you initialized score in Start() then why not initialize Updatescore too? Nevertheless, since they're fields/class data-members then they're initialized to their default values(i.e. 0 for int, false for bool, and null for reference-type fields like string or GameObject).

  • Why not use uGUI? GUI.Box & OnGUI are Legacy UI. Use the new buttons which offer more flexibility and ease of use. Check the two links below for more info.

new UI (uGUI) Intro --- new uGUI's buttons

Comment
Add comment · Show 5 · 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 Jackop222 · Aug 06, 2015 at 09:08 AM 0
Share

I am getting the debug message so i now it is being called but it stays with "Score:0" even with your scipt?

avatar image Hellium · Aug 06, 2015 at 09:18 AM 0
Share

What's the difference between score and UpdateScore ?

In the OnGUI function you are displaying score, but you never modify it, you change the value of UpdateScore ins$$anonymous$$d ...

avatar image Jackop222 · Aug 06, 2015 at 09:21 AM 0
Share

But i set "score = updatescore"

avatar image Major0 · Aug 10, 2015 at 03:16 PM 0
Share

@Jackop222 that is impossible. It worked perfectly here. Perhaps it's the code that is calling it? See how I called it in my answer and try to replicate that.

Perhaps sharing your code that is calling ISCORE() would be a great idea to solve this strange issue.

avatar image Jackop222 · Aug 11, 2015 at 09:43 AM 0
Share

No it is definatly being called, as i am getting the debug message, it is really strange. Here is the code that calls the ISCORE()`using UnityEngine; using System.Collections;

 public class RedLazer : $$anonymous$$onoBehaviour {
     int HereHealth;
     public GameObject Bang;
     private GameObject instantiatedObj;
     public GameObject GameController ;
     public Score score;
     // Use this for initialization
     void Start () {
         HereHealth = 5;
         score = GameController.GetComponent<Score> ();
     }
     
     // Update is called once per frame
     void Update () {
         if (HereHealth <= 0) {
             Destroy (gameObject);
             score.ISCORE();
 
             instantiatedObj = (GameObject) Instantiate(Bang, transform.position, transform.rotation);
             Destroy(instantiatedObj, 0.5f);
         }
     }
     void OnTriggerEnter(Collider col){
         if (col.gameObject.tag == "RLazer") {
             HereHealth --;
             Destroy(col.gameObject);
             
         }
     }
 }
 
 `

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

iOS Lag From Updating Score 0 Answers

How to call a function only once in Update 1 Answer

Problem: Adding and Subtracting score 1 Answer

Score not updating... 1 Answer

How do I Instantiate a prefab to touch and still update a score? 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