- Home /
Show new button with WaitforSeconds
Hello All
Complete Newbie here
I'm trying to add a clickevent on a button1, that will show another button2 after it has waited for 10secs
i used the unity on clicked events which was great, but i have no idea how to add a delay on the 2nd button before it appears.
Can i just write a script that delays its visibility for 10Secs and apply that script to button2?
i have added this script to the button2`enter code here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
StartCoroutine(ButtonDelay());
}
IEnumerator ButtonDelay()
{
print(Time.time);
yield return new WaitForSeconds(10);
print(Time.time);
}
}
` I would be extremely grateful is someone could point me in the right direction
Do you want to make the click of a button make another button appear? Or is your problem to assign the on click function assignement to buttons from a script?
Yes, i want to click one button, and make a new button appear, but the new button to appear once its waited 10 seconds
Answer by Captain_Pineapple · Feb 11, 2019 at 03:11 PM
Well first you need a function that is called when you click the first button. Take a look at the Button Manual Page for this. There is a OnClick List for functions that will be called when clicking the button. Simply click the plus and add an object and a function from some script on the chosen object in there. There is also a ton of tutorials on this out there. For example this one from the Unity base tutorials.
To add a button you have 2 options:
add a prefab that contains a button and give your managing script a reference to this prefab. ( easier to scale - so adding more and more buttons dynamically) or
add a button to the ui and deactivate it on start. Then keep a reference to this second button on the first button and simply set the gameobject active when the button is clicked. (easier to position the button and easier to set up in general - gets messy in large projects)
Hope this helps you to start with. In case you need more information on this let me know what you don't understand and i'll try to help you on.
Answer by MortonPrice · Feb 11, 2019 at 03:24 PM
Thank you Captain i added my events to button 1, which removes itself, and adds Button 2
On Button 2 i added a script to wait for 10 seconds, but still turns on instant
Well ok that way makes things a bit more complicated. What you want to do here is add a parent transform to your second button. To this parent transform you add the script to your second button. (For which you will now think up a clever name that describes it's function please :)) Then in the start function of your script you do 2 things. First is to start the timer coroutine that you already have. Second is to find the button in the children of your script transform and deactivate it (in case it not already is) Then when the timer runs out you only have to activate the transform/gameobject of the second button in the scripts children.
If you keep button 2 and the script running its behaviour on the same transform/gameobject you will have problems when activateing/disabling said object. By keeping them a step apart this makes things easier.
Apologies i'm so lost i have no idea what a parent transform is
No worries, everyone has to start somewhere.
All objects are existent in a hierarchy. Each object in Unity is/has a transform. When you add a transform you can drag/drop it on another object in the heirarchy to make it the child of the first object. Then the upper object is the parent of the lower object and can be accessed by transform.parent
from the child object.
On the other hand you can iterate over all children in a transform by calling:
foreach(Transform t in transform)
{
Debug.Log("I am "+ t.name + " a child of " + transform.name);
}
Read/do some tutorials on this. this will help you understand the unity basics.
Your answer
![](https://koobas.hobune.stream/wayback/20220612191532im_/https://answers.unity.com/themes/thub/images/avi.jpg)