High score script help (Script included)

Hey all,

So here’s my script, i can’t see why it’s not working, but again i’m new to the scripting scene. I have all the code there, i’m trying to center my high score GUI using the GUI Matrix (So on android phones it will adjust to the relevant position and size, aka center) as well as implement the +1 coin count on collision that will change the gui text on collision with the Coin.

I’m sorry if this makes no sense, It’s late here in Australia, and this script is stressing me out :stuck_out_tongue:

Script:

var originalWidth = 1080;  // define here the original resolution
var originalHeight = 1920; // you used to create the GUI contents 
var score = 3f;
private var scale: Vector3;

function OnControllerColliderHit (hit : ControllerColliderHit){

    if(hit.gameObject.name == "Coin")
	
	 {
 
     score = score-1;
 
  }
  
  function Update(){
  
  GameObject.Find("GUI Text").guiText.text = "x"+score;
  
  }
 
function OnGUI(){
    scale.x = Screen.width/originalWidth; // calculate hor scale
    scale.y = Screen.height/originalHeight; // calculate vert scale
    scale.z = 1;
    var svMat = GUI.matrix; // save current matrix
    // substitute matrix - only scale is altered from standard
    GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
    // draw your GUI controls here:
    
    //...
    // restore matrix before returning
    GUI.matrix = svMat; // restore matrix
}

Sorry if its a bit messy, but if you could maybe help that’d be great!

Thank you so much in advance,

-Fersuta

For starters, you never close your OnControllerColliderHit method, you’re missing a } on line 15 there. Otherwise… don’t set your text value in Update, that’s completely unnecessary and wasteful. Set it in the OnControllerColliderHit method. Additionally, don’t look for your GUI Text every time you set your score, look for it once then save it in a private variable.

In pseudocode, it’d look something like

if(guiText == null) {
    guiText = GameObject.FindObjectWithTag("GUIText").GetComponent<GUIText>(); //Don't put spaces in your tags
}

//do stuff with guiText now...

Thanks for the guidance broseph :slight_smile: