Spawn Script Instantiate Method?

Hey,

I am trying to build a tetris clone on my own. I know there are alot of tutorials but I am trying to get things done myself.

I got my prefabs (Blocks) done and also made this prefabs move to the bottom. After that I tried to build the spawn Script, which is attached to my GameController. I used a random.value to determine which block to spawn next. Obviously the instantiate part is repeated quite often, so I decided to make spawn() function. The Script didn’t work that way tho. It works without the function… repeating the instantiate code part in every if statement.

Be aware it looks messy. I tried out alot of stuff and kept changing the code for around 2 hours now.

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {
    public GameObject tBlock;
    public GameObject zBlock;
    public GameObject secondZBlock;
    public GameObject lBlock;
    public GameObject secondLBlock;
    public GameObject iBlock;
    public GameObject sBlock;

    public Vector3 spawnValues;

    public float startWait;
    public float spawnWait;

    

    

	// Use this for initialization
	void Start () {
        StartCoroutine(SpawnBlocks());

    }
	
	// Update is called once per frame
	void Update () {
        
        


    }

    IEnumerator SpawnBlocks()

    // Spawns Waves of asteroids
    {
        yield return new WaitForSeconds(startWait);
        //while (true)
       // {
            for (int i = 0; i < 10; i++)
            {


                float z = Random.value;
                if (z <= 0.2)
                {
                    spawn(tBlock);
                }
                else if (z <= 0.4 && z > 0.2)
                {
                    spawn(sBlock);
                }
                else if (z <= 0.6 && z > 0.4)
                {
                    spawn(iBlock);
                }
                else if (z <= 0.8 && z > 0.6)
                {
                    if (z <= 0.7)
                    {
                        spawn(lBlock);
                    }
                    else
                    {
                        spawn(secondLBlock);
                    }

                }
                else
                {
                    if (z <= 0.9)
                    {
                        spawn(zBlock);
                    }
                    else
                    {
                        spawn(secondZBlock);
                    }
                }
            }
       
        }

    //}
    IEnumerator spawn(GameObject x)
    {
        Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
        Quaternion spawnRotation = Quaternion.identity;
        Instantiate(x, spawnPosition, spawnRotation);
        yield return new WaitForSeconds(spawnWait);
    }
}

Hi,

if you want to call another Courotine and have your original Coroutine to wait until it finished you have to return Coroutine call. So your code will looks like this.

        yield return new WaitForSeconds(startWait);
		while (true)
		{
			for (int i = 0; i < 10; i++)
			{


				float z = Random.value;
				if (z <= 0.2)
				{
					yield return spawn(tBlock);
				}
				else if (z <= 0.4 && z > 0.2)
				{
					yield return spawn(sBlock);
				}
				else if (z <= 0.6 && z > 0.4)
				{
					yield return spawn(iBlock);
				}
				else if (z <= 0.8 && z > 0.6)
				{
					if (z <= 0.7)
					{
						yield return spawn(lBlock);
					}
					else
					{
						yield return spawn(secondLBlock);
					}

				}
				else
				{
					if (z <= 0.9)
					{
						yield return spawn(zBlock);
					}
					else
					{
						yield return spawn(secondZBlock);
					}
				}
			}
		}

In older versions of unity you have to also start unity. So your code needs to be

yield return StartCoroutine(spawn(secondZBlock));