- Home /
Destory button on click and other problem
I'm trying to make a text adventure game and it starts off with a normal button to start the adventure, but I want the button to go away as soon as I click it so it's just text showing. I've used the Destroy method but it doesn't do anything. Here is my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AnimatedDialog : MonoBehaviour
{
public string [] dialog;
public float speed = 0.1f;
private Text textArea;
private bool isSpawned;
private Button startButton;
int stringIndex = 0;
int characterIndex = 0;
private void Start ()
{
StartCoroutine (DisplayTimer ());
textArea = GameObject.Find ("Text").GetComponent<Text> ();
startButton = Button.FindObjectOfType<Button> ();
}
public void OnClick ()
{
if (!isSpawned)
{
Instantiate (this, new Vector3 (0f, 0f), Quaternion.identity);
isSpawned = true;
}
else
{
Destroy (startButton);
return;
}
}
IEnumerator DisplayTimer ()
{
while (1 == 1)
{
yield return new WaitForSeconds (speed);
if (characterIndex > dialog [stringIndex].Length)
{
continue;
}
textArea.text = dialog [stringIndex].Substring (0, characterIndex);
characterIndex++;
}
}
private void Update ()
{
if (Input.GetButtonDown("Jump"))
{
if(characterIndex < dialog [stringIndex].Length)
{
characterIndex = dialog [stringIndex].Length;
}
else if(stringIndex < dialog.Length)
{
stringIndex++;
characterIndex = 0;
}
}
}
}
The other problem I'm having is when I press space to make the text go, it makes another instance of the dialog prefab even though I'm not clicking the button anymore, why is this happening? I've tried making a separate script to just destroy the button but it's still there? Is there something I'm missing about the destroy method?
When you do FindObjectOfType your getting an array of start buttons. Try using GameObject.Find() that should get just one of them. Or try tagging them and do FindWithTag(). i think that solves the not destroying problem..
the other problem, line 41 while (1==1) -> Why is that there?
Button.FindObjectOfType () Returns the first active loaded object of Button type. So it might not be your start button if you have more than one button in your scene. As @Vice_Versa suggested you can use GameObject.Find(). I am also curious about (1 == 1) but I don't think that it is related to your problem. But your instantiate method is a problem. It clones your current object. And I believe your button has a focus on it. That's why when you press space it clicks the button and your instantiate creates a clone.
So I've tried using GameObject.Find() and that doesn't seem to make much of a difference. The start button is still there and it's also the only button in the scene. As for the while (1 == 1) everyone's asking about that's there so the dialog will display all the characters of the string.
If anyone could explain to me why the button thinks i'm clicking on it anytime I hit space that would also be nice.
Also I appreciate the explanations for everything as well! Always nice to learn something new!
Well, after clicking a button, if I press space, it clicks the button and that's because of the navigation of the button. If this is your case, go to your button and change the navigation to something else. It is auto by default. Hope this helps a bit.
Thank you! this did the trick to keep it from spawning more!