How to spawn building for 5 second after user click button?

Whenever I coded for spawning building when the use click a button, instead of waiting 5 seconds before the buildings, my coding become such that whenever the timing is less than 5 seconds, the buildings would not spawn and it would only create when the timing is more than 5 seconds.

My idea is that when instead of waiting 5 seconds before the building is created instantly when user create button, my idea is to when the user create the building, it would wait 5 seconds before the building automatuically spawn when the user create the button.I dun have the code right now since i left it in school, but I would post it tomorrow morning but if got any idea, do give me advice.

Ok lets clear things up.
What exactly do you need?

  1. Click - Wait 5 sec - Spawn
  2. click - Spawn - Wait 5 sec - Do something else

However things you should look up are Coroutines and Invoke.

E.g. for case 1):

if(Input.GetMouseButtonDown(0)){
Invoke("SpawnBuilding", 5);
}
  1. Click building icon, wait 5 sec then the building would spawn.

Ok look at the example i gave above…that does exactly that, or tell if you need further explanation.

Ref:

My code is like that.

GameObject SchoolTiming = GameObject.Find ("CurrentTime");
SchoolScript s1 = GetComponent<SchoolScript>();
					
GameObject SchoolStartTiming = GameObject.Find ("StartTime");
SchoolScript s1a = GetComponent<SchoolScript>();

GenerateTiles script = GetComponent<GenerateTiles>();
					
					
if(!script.CheckOccupied(hit.collider.gameObject, 2, 2))
{
						
							
						
	script.SetOccupied(hit.collider.gameObject ,2, 2);
	
	if(s1.CurrentTime  >= 5) 
	{
		
								
			Instantiate (school, hit.collider.gameObject.transform.position, Quaternion.identity);
								
	}
						
}

InputScript file

// Update is called once per frame
void Update () {
		StartTime = Time.time;
		//SchoolSeconds += Time.deltaTime;
		CurrentTime = StartTime - 0 ;
	
}

SchoolScript file

So, in the end, what is wrong with my code. How to make it such that when the user institatite and click the button, the program would wait 5 seconds before spawning the buildings.

Help!!!

I believe Arterie has answered your question. Invoke will call the function after a certain amount of delay. So all you have to do is to define the function SpawnBuilding as shown in Arterie’s example. Then call Invoke when your button has been clicked.

    if(s1.CurrentTime  >= 5) 
    {
            Instantiate (school, hit.collider.gameObject.transform.position, Quaternion.identity); 
    }

Using invoke here instead of the if-clause should work just fine.
Though as wolfhunter said you need to define a SpawnBuilding function where you enclose the Instantiate call.

public GameObject house;
public float SpawnDelay;

void Update() {
	if (Input.GetMouseButtonDown(0)) {
		StartCoroutine(SpawnBuilding(SpawnDelay));
	}
}

public IEnumerator SpawnBuilding(float waitTime) {
	yield return new WaitForSeconds(waitTime);
	GameObject.Instantiate(house, Vector3.zero, Quaternion.identity);
}

just do

function Update () {

    if(Input.GetMouseButtonDown(0)){

       yield WaitforSeconds (5);
       //spawn building

      }
}