-1

I have create UIManager Script and Initialize Text handle and assign at start but when i play the filed is empty

enter image description here

i have find similar question but the answer is not clear to me here my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    [SerializeField]
    private Text _scoreText;

    private void Awake()
    {
        _scoreText = GetComponent<Text>();
    }
    void Start()
    {
       _scoreText.text = "Score" + 0; 
    }

    // Update is called once per frame
  public void UpdateScore(int playerScore)
    {
        _scoreText.text = "Score" + playerScore.ToString();
    }
}
suleiman
  • 29
  • 1
  • 1
  • 7
  • 3
    Your gameobject does not seem to include a 'Text'-component. On Awake you look for that component type and you try to assign it. But since you don't appear to have one on the object, you'll assign null to your variable. – Florian H. Jun 17 '21 at 04:59
  • am answering my self since the UIManager don't have the text compononent I have to declare a handle for component am looking for public Text _text; Then assign the handler in Start method _text = GameObject.Find("UI_Manager").GetComponent(); then now I can access the component but i have to drag the text component from canvas to field in UI_Manager – suleiman Sep 25 '21 at 13:41

3 Answers3

2
GetComponent<Text>();

Gets the component "Text" from that gameObject which you have the script on. And looking at that image you have no "Text" component on that gameObject.

You could either add the component to it by clicking that Add Component button at the bottom of that gameObject or if you want to get a "Text" component from a different gameObject you could just drag and drop that gameObject into the Editor.

Vasil3
  • 100
  • 7
1

Before getting the Text component, first add Text component to your object or assign it.

  • You can do it like: gameObject.AddComponent<Text>();

Don't forget that Text is a component of UI, so it should have Canvas on the top of the hierarchy

Akhrorjon
  • 136
  • 9
1

But when I play the field is empty.

This happens because GameObject.GetComponent() searches for the component on the GameObject you are calling it from and as you can clearly see on your screenshot the UIManager doesn't include a Text object.

You seem to have assigned the Text component directly that is correct, but what you have to think about is that you are resetting the variable in Awake because you replace it with null.

What happens in your code:

[SerializeField]
private Text _scoreText; // Assigned in inspector.

private void Awake() {
    // Sets scoreText to null, because the UIManager has no Component Text.
    _scoreText = GetComponent<Text>();
}

Instead you should just remove the code in the Awake method all together and rather add a check to ensure it's set in the inspector.

Replaced Awake Method:

private void Awake() {
    // Check if scoreText was assigned in the inspector.
    if (_scoreText == null) {
        Debug.LogWarning("ScoreText has not been assigned in the inspector.");
        // Get the gameObject with the given name.
        GameObject textGo = GameObject.Find("Text GameObject Name");
        // Attempt to get the component from the given GameObject.
        if (textGo.TryGetComponent(out Text text)) {
            _scoreText = text;
        }
    }
}

If it isn't set you could always use GameObject.Find() combined with GameObject.TryGetComponent() to attempt to get the Text Component from the GameObject with the given name.

IndieGameDev
  • 2,905
  • 3
  • 16
  • 29
  • Using `FindComponentOfType()` for this is not the best idea .. there is often more than one single text in a scene ... – derHugo Jun 17 '21 at 06:12
  • @derHugo Thanks for the feedback adjusted it to use Find and TryGetComponent now. Should have the same result and be more or less completely fool proof. – IndieGameDev Jun 17 '21 at 11:02
  • Thanks i have same issue even after replace the awake code with your code any idea why – suleiman Jun 17 '21 at 18:33
  • @suleiman Do you get the Warning in the inspector ```"ScoreText has not been assigned in the inspector."```? If you do are you sure you changed the ```string``` in ```GameObject.Find("Text GameObject Name");``` to the name your ```gameobject``` with the ```text``` component on it has. For example ```GameObject.Find("ScoreText");``` – IndieGameDev Jun 18 '21 at 05:46