Have two scripts to pick up coins from a minigame, same scripts I used in other games but for some reason, not working here, im still new to this so.. here.
public class Monedos : MonoBehaviour
{
public static int Score;
public GameObject ScoreText;
// Start is called before the first frame update
void Update()
{
ScoreText.GetComponent<Text>().text = "" Score;
}
}
And the other one
public class CollectGold : MonoBehaviour
{
public AudioSource OroFX;
void OnTriggerEnter(Collider other)
{
OroFX.Play();
Monedos.Score ;
this.gameObject.SetActive(false);
}
}
Everything is set in the game as it should, but game isnt working, error :
NullReferenceException: Object reference not set to an instance of an object
Monedos.Update () (at Assets/Scripts/Monedos.cs:15)
First i was thinking this was cuz of textmeshpro but text legacy isnt working either so..
Changed Text to textmeshpro, modified the gameobjects to make sure I wasnt using the wrong ones, actually copy and pasted from working games and still, nothing working.
CodePudding user response:
Looks like you forgot to put a link for ScoreText in inspector for Monedos, or ScoreText does not have a component which type is Text.
By the way, static is a kinda bad practise, so you can put a field type of Monedos into CollectGold, then put a link on it in inspector, so you would able do get the Score value from Monedos instance.
And setting text for some text component in Update method for each frame is a bad practise. Make a method for setting it and call it only when you need to change text