I’m working on a Unity project where I need to change the text on my objects after an animation ends. Despite my efforts, the loop doesn’t seem to proceed to the next iteration correctly, and the text on the objects does not update as expected.
Description of the problem:
The script is supposed to update the text on the carrot objects after each question. However, after the animation ends, the text does not update as expected. It seems like the loop does not move to the next iteration correctly.
What I’ve tried:
Debugging to ensure the correct flow of logic in the nextQuestion coroutine.
Checking the animator and animation states to confirm they are triggering and ending correctly.
Ensuring the text components are correctly referenced and accessible.
Could you help me find the issue that prevents the loop from going to the next iteration and updating the text correctly?
Thank you!
Here is the relevant portion of my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Bunny : MonoBehaviour
{
public GameObject carrots;
public float speed; // Movement speed of the object
public float currentYrotation;
public string[] questions, answer_keys; // Array of questions and answer keys
string[] answers;
int number = 0;
public TMP_Text question_display;
void Start()
{
StartCoroutine(nextQuestion());
}
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
// Move left along the Z axis
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, speed);
transform.rotation = Quaternion.Euler(0, currentYrotation - 20, 0);
}
if (Input.GetKey(KeyCode.RightArrow))
{
// Move right along the Z axis
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, -speed);
transform.rotation = Quaternion.Euler(0, currentYrotation + 20, 0);
}
if (Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.LeftArrow))
{
// Stop movement along the Z axis
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
transform.rotation = Quaternion.Euler(0, currentYrotation, 0);
}
}
void OnTriggerEnter(Collider obj)
{
if (obj.name == "carrots")
{
if (obj.transform.GetChild(0).GetComponent<TMP_Text>().text == answers[0])
{
print("correct");
}
else
{
print("wrong");
}
StartCoroutine(nextQuestion());
}
}
IEnumerator nextQuestion()
{
yield return new WaitForSeconds(0.25f);
number++;
question_display.transform.parent.gameObject.SetActive(true);
question_display.text = questions[number];
carrots.GetComponent<Animator>().enabled = true;
carrots.GetComponent<Animator>().Play("carrots");
answers = answer_keys[number].Split('|');
for (int i = 0; i < carrots.transform.childCount; i++)
{
carrots.transform.GetChild(i).GetChild(0).GetComponent<TMP_Text>().text = "";
}
for (int i = 0; i < carrots.transform.childCount; i++)
{
int index;
do
{
index = (int)Random.Range(0, 2.4f);
} while (carrots.transform.GetChild(index).GetChild(0).GetComponent<TMP_Text>().text != "");
carrots.transform.GetChild(index).GetChild(0).GetComponent<TMP_Text>().text = answers[i];
}
}
}