Help me with text array.
Hi I'm new to programming in unity and I need some help. I'm trying to create an array that holds in text and only one of the texts in the array is enabled (SetActive) once I have clicked the left mouse button the current text that will be disabled and the next text in the array is going to be enabled but I'm having trouble with that this is what I have got so far:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Tutorial : MonoBehaviour {
[SerializeField] GameObject[] tutorialText;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
int tutorialTextLength = tutorialText.Length;
int textIndex = tutorialTextLength;
if(Input.GetMouseButtonDown(0))
{
}
}
}
PLEASE HELP
Answer by eses · Aug 24, 2018 at 07:22 PM
You'll have to learn the basics of loops and iteration.
However, you could do something like this:
public GameObject[] tutorialText;
public int index = 0;
public bool tutorialDone = false;
void Start ()
{
foreach (var item in tutorialText)
item.SetActive(false);
}
void Update()
{
if (Input.GetMouseButtonDown(0) && !tutorialDone)
ShowNextText();
}
void ShowNextText()
{
if (index < tutorialText.Length)
{
Debug.Log("tick");
tutorialText[index].SetActive(true);
if (index > 0)
{
tutorialText[index-1].SetActive(false);
}
index++;
}
else if (index == tutorialText.Length)
{
tutorialText[index-1].SetActive(false);
tutorialDone = true;
}
}
This would show the first object with first click, then the next one. When the last one is shown, click will hide it and the tutorial would be done.
Thank you very much this helped a lot. Again I'm new to program$$anonymous$$g and I had no idea what to do but you look like you know what's up so again thanks!
@Dimitry_Bogov - good - could you please mark the answer as correct answer if you think it is the correct answer - thanks.