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.