I am making a game where the game shows an equation at the top and at the bottom,
It spawns 3 fruits from the bottom and each of them has a number on it. Right now I have an empty object and labeled it answerText
, then added a TextMeshProUGUI
component to it, then moved it into my prefabs.
When I run the game, I see a text icon right above the fruit spawning, but in the game view, I don’t see anything. Here is my code for the script where I am spawning the fruit. I am trying to make the text follow right above the fruit. If anybody has any suggestions I would really appreciate it.
IEnumerator SpawnTarget()
{
while (true)
{
yield return new WaitForSeconds(spawnRate);
int randomIndex = Random.Range(0, equationList.Count);
string randomItem = equationList[randomIndex];
string randomAnswer = answerList[randomIndex];
Vector3 spawnPosition = RandomSpawnPos();
GameObject fruit = Instantiate(targets[Random.Range(0, targets.Count)], spawnPosition, Quaternion.identity);
Rigidbody fruitRigidbody = fruit.GetComponent<Rigidbody>();
fruitRigidbody.AddForce(RandomForce(), ForceMode.Impulse);
fruitRigidbody.AddTorque(Vector3.forward * RandomTorque(), ForceMode.Impulse);
GameObject answerTextObj = Instantiate(answerTextPrefab, fruit.transform.position + answerTextOffset, Quaternion.identity);
answerTextObj.transform.SetParent(fruit.transform);
TextMeshProUGUI answerText = answerTextObj.GetComponent<TextMeshProUGUI>();
answerText.text = randomAnswer;
}
}
I am using a textmeshpro-text(UI) for the answer Text. Here is an image
I’m not sure if something is wrong with the canvas, but I will appreciate any feedback or comments.