How to make an action wait 3 seconds after start

Hello everyone, i was just wondering if someone might be able to point me in the right direction on how to accomplish this. i have 6 triangles in my game. it is a single scene game. They are all tagged with “TriangleP”. They also have Box Collider 2D. i created a script to randomize which triangle is assigned to a moving script (from itween). However my question is, how do i hold off the action of one of the triangles moving for 3 seconds because i will have a 3 second countdown timer at the start of the game and once it finishes then the randomized action will take place.

using UnityEngine;
using System.Collections;

public class RandomizeScript : MonoBehaviour {




	public GameObject[] Triangles;



	// Use this for initialization
	void Start () {


		Triangles = GameObject.FindGameObjectsWithTag ("TriangleP");
		Triangles [Random.Range (0, Triangles.Length)].AddComponent<MoveSample> ();
	}
	
	// Update is called once per frame
	void Update () {
	StartCoroutine("waitThreeSeconds");

}

	IEnumerator waitThreeSeconds(){
		yield return new WaitForSeconds(3);

	}
}

I tried using a Coroutine method however it doesn’t seem to work, and for those of you wondering, yes i am new to unity and just plain coding in general. I’ve been at it for a month now and i plan to continue down the road. Any help at all will be greatly appreciated.

I don’t know what your “MoveSample” component does, but I’m assuming that it starts moving the triangle immediately after being added?

You definitely don’t want to be starting a coroutine every single frame.

Any logic you want to happen after 3 seconds should come after the WaitForSeconds line in the coroutine.

Try this:

 using UnityEngine;
 using System.Collections;
 
 public class RandomizeScript : MonoBehaviour {
	public GameObject[] Triangles;

	// Use this for initialization
	void Start() {
		Triangles = GameObject.FindGameObjectsWithTag("TriangleP");
		StartCoroutine(waitThreeSeconds());
	}

	IEnumerator waitThreeSeconds() {
		yield return new WaitForSeconds(3);
		Triangles[Random.Range(0, Triangles.Length)].AddComponent<MoveSample>();

	}
}

Be sure to save your project. Coroutines tends to crash Unity if you do it wrong.

From the information you gave this is what I can show you. Also, you may want to go through coroutine tutorials in Unity Learn site. Click Here.

using UnityEngine;
using System.Collections;

public class RandomizeScript : MonoBehaviour
{

    public GameObject[] Triangles;

    // Use this for initialization
    void Start()
    {
       Triangles = GameObject.FindGameObjectsWithTag("TriangleP");
       Triangles[Random.Range(0, Triangles.Length)].AddComponent<MoveSample>();//I am assuming "movesample" moves the object??
        StartCoroutine("waitThreeSeconds");
    }

    // Update is called once per frame
    void Update()
    {
        //you don't want a new coroutine to start every frame do you?
        //StartCoroutine("waitThreeSeconds"); <--- Don't place this in Update
    }

    IEnumerator waitThreeSeconds()
    {
        while (true)
        {
             
            //Perform the action, or the move
            //Not sure what's in "moveSample" script so can't tell you where this should go
            yield return new WaitForSeconds(3); //wait 3 seconds
            Debug.Log("3 seconds has passed since the action");
            //Perform "random action"           
            yield return null; //Yield
        }

    }
}