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?

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) Intronew uGUI’s buttons

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 ?