This is my code to spawn a random topic:
public TextMeshProUGUI topicText;
private string[] allTopics = new string[9];
public int topicIndex;
// Start is called before the first frame update
void Start()
{
allTopics[0] = "Food";
allTopics[1] = "Habitats";
allTopics[2] = "Personality";
allTopics[3] = "Toxic Objects";
allTopics[4] = "Exercise";
allTopics[5] = "Hygiene";
allTopics[6] = "Enrichment";
allTopics[7] = "Health Screening";
allTopics[8] = "Materials";
topicIndex = Random.Range(0, 8);
topicText.text = allTopics[topicIndex];
}
I am trying to access topicIndex from another script. I tried:
topicIndex = GameObject.FindGameObjectWithTag("Topic").GetComponent<TopicSpawn>().topicIndex;
2.public GameObject topic; topicIndex=topic.GetComponent<TopicSpawn>().topicIndex;
3.Edit the topicIndex statement in the script topicSpawn, by changing topublic static int topicIndex;
then access it withTopicSpawn.topicIndex;
Nothing has worked so far, with topicIndex=0 on my new script even though topicIndex on my original script, TopicSpawn is not zero…
How can I fix it? Thank you.