need help Pausing random.range

Hello Again lovely community.
I am trying to spawn Blocks specifik Places randomly and for that i used random.range. However, It’s too fast so i wanted to yield it for x (in this case 5) seconds. But when i run this script i get this error message:

Assets/Scripts/MovementUP.cs(17,15): error CS1624: The body of MovementUP.respawm()' cannot be an iterator block because float’ is not an iterator interface type

So yeah that’s a thing.
Here’s the script. It’s a bit undone but as far as i know it doesn’t matter cause the problem is with the yielding (i am quite sure. Beats me which is why i am asking you Guys XD )

using UnityEngine;
using System.Collections;

public class MovementUP : MonoBehaviour {
    new public GameObject Rocket;
    new Vector3 StartPos = new Vector3 (0, -3, 0);
    new Vector3 RightPos = new Vector3 (6,-3,0);
    new Vector3 LeftPos = new Vector3 (-6,-3,0);
    new public GameObject Block;
    new Vector3 Middle = new Vector3 (0, 6, 0);
    new Vector3 Right = new Vector3 (6, 6,0);
    new Vector3 Left = new Vector3 (-6, 6,0);
    new int i = 5;



    float respawm()
    {
   
        int number = Random.Range (1, 3);
        yield return new WaitForSeconds (5);
        number = Random.Range (1, 3);
        return number;
    }

    // Use this for initialization
    void Start () {
   
        Rocket.transform.position = StartPos;




    }

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


   
        if (random (1, 3) == 1) {
            Instantiate (Block, Middle, new Quaternion(0, 0, 0, 0));
                }

   
   
                if (Input.GetKey (KeyCode.D)) {
                        Rocket.transform.position = RightPos;

                       
                } else if (Input.GetKey (KeyCode.A)) {
                        Rocket.transform.position = LeftPos;
       
                }

         else if ((Input.GetKey(KeyCode.A) == false ) & (Input.GetKey(KeyCode.D) == false))
        {
            Rocket.transform.position = new Vector3 (0,-3,0);
                }
               


    }
}

You need to use coroutines to call for a yield.

check Unity - Manual: Coroutines

Hope it helps!

try
IEnumerator respawn()

I tried it so the code is now:

using UnityEngine;
using System.Collections;

public class MovementUP : MonoBehaviour {
    new public GameObject Rocket;
    new Vector3 StartPos = new Vector3 (0, -3, 0);
    new Vector3 RightPos = new Vector3 (6,-3,0);
    new Vector3 LeftPos = new Vector3 (-6,-3,0);
    new public GameObject Block;
    new Vector3 Middle = new Vector3 (0, 6, 0);
    new Vector3 Right = new Vector3 (6, 6,0);
    new Vector3 Left = new Vector3 (-6, 6,0);
    new int i = 5;



    IEnumerator respawm()
    {
   
        int number = Random.Range (1, 3);
        yield return new WaitForSeconds (5);
        number = Random.Range (1, 3);
        return number;
    }

    // Use this for initialization
    void Start () {
   
        Rocket.transform.position = StartPos;




    }

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


   
        if (StartCoroutine(respawn) (1, 3) == 1) {
            Instantiate (Block, Middle, new Quaternion(0, 0, 0, 0));
                }

   
   
                if (Input.GetKey (KeyCode.D)) {
                        Rocket.transform.position = RightPos;

                       
                } else if (Input.GetKey (KeyCode.A)) {
                        Rocket.transform.position = LeftPos;
       
                }

         else if ((Input.GetKey(KeyCode.A) == false ) & (Input.GetKey(KeyCode.D) == false))
        {
            Rocket.transform.position = new Vector3 (0,-3,0);
                }
               


    }
}

But when i run this code it gives me this error message:

Assets/Scripts/MovementUP.cs(23,17): error CS1622: Cannot return a value from iterators. Use the yield return statement to return a value, or yield break to end the iteration

I really am hopeless when it comes to this so any help will be nice :slight_smile:

The error says this: “Cannot return a value from iterators”.

You are returning a value from something that is called an Iterator, namely in the IEnumerator respawm. Remove the “return number;” line to not do that.