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!

  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);
     }
 }

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();

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