#how do i reference a script thats inside

1 messages · Page 1 of 1 (latest)

jagged pagoda
#

You mean attached to a prefab ? Where do you want to access it from ?

stiff acorn
#

im trying to access a variable thats inside a script of a prefab from another game object

#
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class ScoreUI : MonoBehaviour
{
    public Score score;
    TextMeshProUGUI textMesh;
    // Start is called before the first frame update
    void Start()
    {
        textMesh = GetComponent<TextMeshProUGUI>();
        textMesh.text = ($"Score: {score.currentScore}");
    }

    // Update is called once per frame
    void Update()
    {

    }
}```
Score is the script thats inside a prefab but everytime i try to start the game theres an error
jagged pagoda
#

What's your error ?

stiff acorn
#

null reference exception

jagged pagoda
#

I suppose it's a nullRef since you're trying to access Score

#

Well, you'll need a ref to the instantiated gameObject that holds Score, then you can call GetComponent<Score>() on it

#

The simplest (but not the best) way would be to use (Score)Object.FindObjectOfType(typeOf(Score)) in your Start()

stiff acorn
#

that doesnt work

jagged pagoda
#

When is your prefab holding the Score script instantiated in your Scene ?

#

Is it already there when you press play ?

stiff acorn
#

yes

#

wait no

#

its not

jagged pagoda
#

OK, so that's because Start() is called before your prefab even exist in the scene

#

Are you familiar with the singleton pattern ?

stiff acorn
#

uh no

jagged pagoda
#

Give me a minute, I'll try to give you an example

stiff acorn
#

uh okay now im not getting the error

#

but now it doesnt show on the text

#

it does print tho

jagged pagoda
#
public class ScoreUI : MonoBehaviour
{
    // Singleton : Here we have a static ref that will be globally accessible
    public static ScoreUI Instance { get; private set; }

    private TextMeshProUGUI textMesh;

    // When you're initializing your ref, better do it in Awake
    private void Awake()
    {
        /* Singleton : For a singleton, we have to ensure we have a single existing instance
        /* so we cache the ref as Instance if there isn't already one existing,
        /* and destroy it otherwise
        */
        if(Instance == null)
        {
            Instance = this;
        }
        else if(Instance != this)
        {
            Destroy(this);
        }

        textMesh = GetComponent<TextMeshProUGUI>();
    }

    // I assume the score is an int, but you can change the type accordingly
    public void UpdateScore(int newScore)
    {
        // Make sure your textMesh actually exist
        if(textMesh == null)
        {
            return;
        }

        textMesh.text = $"Score: {newScore}";
    }
}
#

so let me explain a little bit

#

you'll reverse the responsability

#

instead of having your ui know your Score holder

#

you'll have your score holder tell the UI to update

#

so when your score change, use ScoreUI.Instance.UpdateScore(currentScore) and it should update properly