- Home /
Inspector changes set TextMeshPro values on play
Im really stuck here and struggling to figure out what the hell is going on.
I have this script attached to a gameobject. It points to various TextMeshPro UI elements and in theory (I have done this before with standard text and it worked fine) it should work, but when I hit play, the inspector gets rid of the things I have set. I have included screenshots as I dont know how to word this properly. Thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Questions : MonoBehaviour
{
public TextMeshProUGUI question;
public TextMeshProUGUI answera;
public TextMeshProUGUI answerb;
public TextMeshProUGUI answerc;
public string questiontext;
private string answeratext;
private string answerbtext;
private string answerctext;
void Start()
{
question = GetComponent<TextMeshProUGUI>();
answera = GetComponent<TextMeshProUGUI>();
answerb = GetComponent<TextMeshProUGUI>();
answerc = GetComponent<TextMeshProUGUI>();
questiontext = "When asked to dice an onion, or similar ingredient. What shape do you expect to find the final product?";
answeratext = "cubes";
answerbtext = "strips";
answerctext = "rounds";
}
void Update()
{
question.text = questiontext.ToString();
answera.text = answeratext.ToString();
answerb.text = answerbtext.ToString();
answerc.text = answerctext.ToString();
}
}
If you're setting them in the inspector, then why make those GetComponent calls at all? You seem to already have the T$$anonymous$$P components you want via inspector assignment.
Answer by Esteem · Jan 20, 2020 at 08:19 PM
when you set your textmeshpro variables in editor, don't override them when you start the game.
void Start()
{
question = GetComponent<TextMeshProUGUI>();
answera = GetComponent<TextMeshProUGUI>();
answerb = GetComponent<TextMeshProUGUI>();
answerc = GetComponent<TextMeshProUGUI>();
}
get rid of that bit, you're trying to reset your TextMeshProUGUI components to a component that you;re trying to get on an object that has the Questions script attached to it. (and they're all null because that object doesn't have any TextMeshProUGUI components on itself)
Unity can be a bit scary when you're new, but it gets much easier with time, keep learning!
Your answer
Follow this Question
Related Questions
Display the same string in multiple text boxes 1 Answer
Problems in the script for making a text disappear after few seconds 2 Answers
Unity 2D - Text UI and Textbox scripting issue - HELP ASAP! 2 Answers
How to change color of animated Text component 2 Answers
Best way to defining scripts within a list inside the editor? 1 Answer