C# Waitforsec and Instanciate

Greetings Unity Community :wink:

I hope someone couldt help me out here.

I am trying to make an c# script so when u load the game , it waits for x-secs and then spawn players in the right place …
For the test i just used Cubes, and made them prefabs so i don’t need to type in the transform of the cubes.

i just want to know if i am going in the right way , here is what i have done so far

using UnityEngine;
using System.Collections;

public class SpawnTest : MonoBehaviour {
    IEnumerator Do() {
        print("Jeg starter med at tælle");
        yield return new WaitForSeconds(5);
        print("Jeg køre stadig");
    }
    void spawnTest() {
        Do();
        print("This is printed immediately");
		
		if (Do)
			
			{		
  
			(GameObject)Instantiate.Load("Player1test"));
	    	}
			
			
			
			
    }
}

SO ty in advance for hints or help

Regards Niko

You don’t use constructors like that using Unity - you put initialisation code in either Awake() or Start().

Awake always happens and Start happens just before the object is displayed for the first time.

The benefit of start is that it can just be an coroutine so your code should look a bit like this:

public class SpawnTest : MonoBehaviour
{

  IEnumerator Start()
  {
     yield return new WaitForSeconds(5);
     Instantiate(cube, transform.position, transform.rotation);
  }
}

You have to atach the script to the positon that you want to appears the object in the level charged (Create an empty GameObject in that position), and in the script atach the GameObject that you want to instanciate

using UnityEngine;
        using System.Collections;
         
        public class SpawnTest : MonoBehaviour {
    
    public GameObject cube;
            IEnumerator Do() {
            print("Jeg starter med at tælle");
            yield return new WaitForSeconds(5);
            print("Jeg køre stadig");
        }
        void spawnTest() {
            StartCoroutine("Do");
            print("This is printed immediately");
            Instantiate(cube, gameObject.transform.position,gameObject.transform.rotation);
        }
    }