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:
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 ()
Instantiate (this, new Vector3 (0f, 0f), Quaternion.identity);
isSpawned = true;
IEnumerator DisplayTimer ()
while (1 == 1)
yield return new WaitForSeconds (speed);
if (characterIndex > dialog [stringIndex].Length)
textArea.text = dialog [stringIndex].Substring (0, characterIndex);
private void Update ()
if(characterIndex < dialog [stringIndex].Length)
characterIndex = dialog [stringIndex].Length;
else if(stringIndex < dialog.Length)
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!
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
112 People are following this question.
Change the text of a UI button on clicking it?
Write in button
Dynamically generate buttons from a single text component
I need to resize my text to button to be visible using Unity 4.6 UI
How To Change Color Of Text On UI When It's Selected | Unity 4.6