#Errors getting text in UI.

1 messages · Page 1 of 1 (latest)

honest musk
#

Welp. First time making a thread so I guess we'll figure this out as we go.
Long story short I'm making a text-based game, and I've had nothing but struggles getting the text to show up in the UI. I have a handful of scripts which I'll try and explain their purpose. Or at least... how I believe they're working.

This is the UserInterfaceManager script we've created, from my understanding it's going to be handling most if not all of the UI functionality.

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

public class UserInterfaceManager : MonoBehaviour
{
    public UserInterfaceText mainText;

    public UserInterfaceButton button1;
    public UserInterfaceButton button2;
    public UserInterfaceButton button3;
    public UserInterfaceButton button4;
    public UserInterfaceButton button5;
    public UserInterfaceButton button6;
    public UserInterfaceButton button7;
    public UserInterfaceButton button8;
    public UserInterfaceButton button9;
    public UserInterfaceButton button10;
    public UserInterfaceButton button11;
    public UserInterfaceButton button12;

    
    private void Awake()
    {
        mainText.Text = "Welcome to my game";
        button1.Text = "Button 1";
        button2.Text = "Button 2";
        button3.Text = "Button 3";
        button4.Text = "Button 4";
        button5.Text = "Button 5";
        button6.Text = "Button 6";
        button7.Text = "Button 7";
        button8.Text = "Button 8";
        button9.Text = "Button 9";
        button10.Text = "Button 10";
        button11.Text = "Button 11";
        button12.Text = "Button 12";
    }
}```

The UserInterfaceManager is pulling from both the UserInterfaceButton script and the UserInterfaceText script. My thought is when I hit play (in Unity), the buttons will show their corresponding text. "Button 1" etc... and the main text portion of the UI will say "Welcome to my game". I keep getting NullReferenceException errors.

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

public class UserInterfaceButton : MonoBehaviour
{
    // Fields
    private Text textObject;
    private Button buttonObject;
    private Image imageObject;
    
    // Property
    public string Text
    {
        get
        {
            return textObject.text;
        }
        set
        {
            textObject.text = value;
        }
    }

    public Color Color
    {
        get
        {
            return imageObject.color;
        }
        set
        {
            imageObject.color = value;
        }
    }

    void Awake()
    {
        Initialize();
    }

    public void Initialize()
    {
        // Get the text child object and find it's Text component.
        textObject = transform.Find("Text").GetComponent<Text>();

        buttonObject = GetComponent<Button>();
        imageObject = GetComponent<Image>();
    }
}```

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

public class UserInterfaceButton : MonoBehaviour
{
    // Fields
    private Text textObject;
    private Button buttonObject;
    private Image imageObject;
    
    // Property
    public string Text
    {
        get
        {
            return textObject.text;
        }
        set
        {
            textObject.text = value;
        }
    }

    public Color Color
    {
        get
        {
            return imageObject.color;
        }
        set
        {
            imageObject.color = value;
        }
    }

    void Awake()
    {
        Initialize();
    }

    public void Initialize()
    {
        // Get the text child object and find it's Text component.
        textObject = transform.Find("Text").GetComponent<Text>();

        buttonObject = GetComponent<Button>();
        imageObject = GetComponent<Image>();
    }
}``` 

I'll include pictures assuming I can post them in Threads.
#

Ope. Scratch that. We have text in the text box now, but still have the 3 NRE errors.

loud bear
#

can you show me your Hierarchy and Component...

honest musk
loud bear
honest musk
#

I'm not saying you're wrong, but just to help me wrap my head around it, could you explain why? My Unity experience is far less than my C# experience

loud bear
honest musk
#

So I added the UserInterfaceManager and assigned the Main Text and buttons to their corresponding scripts

loud bear
honest musk
#

None went away, in fact, we added another.

loud bear
cloud shard
honest musk
loud bear
honest musk
#

the line is:
set => textObject.text = value;"

So I'm imagining the object is "textObject.text"

loud bear
cloud shard
#

Have you done what I said?

#

Because without it, you have no idea whether the code you wrote will run and assign the variables

honest musk
cloud shard
#

you declare it

#

instead of Awake

#

that way you know things exist by the time Start rolls around