Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Coroutine and instantiate problem!

Discussion in 'Editor & General Support' started by XP20t, Jan 4, 2019.

  1. XP20t

    XP20t

    Joined:
    Jan 4, 2019
    Posts:
    8
    I have a problem in unity C# with my code, I have an error in instantiate inside a coroutine! Here's my code:

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class Enemies : MonoBehaviour
    5. {
    6.     public float betweenSpawns;
    7.     public bool stop;
    8.     public GameObject Prefab;
    9.     public Transform enemy;
    10.     public PlayerMovement movement;
    11.  
    12.  
    13.     public void OnCollisionEnter(Collision col)
    14.     {
    15.         //Debug.Log(gameObject.name + " Has collided with " + col.gameObject.name);
    16.         Time.timeScale = 0.5f;
    17.         stop = true;
    18.         if (Input.GetKey("r"))
    19.         {
    20.             Time.timeScale = 1;
    21.             stop = false;
    22.  
    23.         }
    24.     }
    25.  
    26.    
    27.    
    28.  
    29.  
    30.  
    31.     // Start is called before the first frame update
    32.     void Start()
    33.     {
    34.         StartCoroutine(Spawner(enemy));
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     IEnumerator Spawner(Transform enemy)
    39.     {
    40.  
    41.  
    42.         while (!stop)
    43.         {
    44.  
    45.             Vector3 spawnPosition1 = new Vector3(Random.Range(-37.5f, -18.75f), 25, 0);
    46.             Vector3 spawnPosition2 = new Vector3(Random.Range(-18.75f, 0), 25, 0);
    47.             Vector3 spawnPosition3 = new Vector3(Random.Range(0, 18.75f), 25, 0);
    48.             Vector3 spawnPosition4 = new Vector3(Random.Range(18.75f, 37.5f), 25, 0);
    49.  
    50.             //here problem with instantiate!
    51.             Instantiate(Prefab, spawnPosition1 + transform.TransformPoint(0, 0, 0), 0, 0, 0);
    52.             Instantiate(Prefab, spawnPosition2 + transform.TransformPoint(0, 0, 0), 0, 0, 0);
    53.             Instantiate(Prefab, spawnPosition3 + transform.TransformPoint(0, 0, 0), 0, 0, 0);
    54.             Instantiate(Prefab, spawnPosition4 + transform.TransformPoint(0, 0, 0), 0, 0, 0);
    55.  
    56.  
    57.             yield return new WaitForSeconds(betweenSpawns);
    58.         }
    59.  
    60.     }
    61. }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    What is the error, and on what line is the error?
     
  3. XP20t

    XP20t

    Joined:
    Jan 4, 2019
    Posts:
    8
    51 - 54
     
  4. XP20t

    XP20t

    Joined:
    Jan 4, 2019
    Posts:
    8
    and problem is no overload for method instantiate takes 5 arguments
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  6. XP20t

    XP20t

    Joined:
    Jan 4, 2019
    Posts:
    8
    thanks but I am new to unity so I don't know much and I don't know what I exactly need to change.
     
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You need to change the number of parameters. Review the link that I provided for the specific syntax. Can you clarify what you intended the + sign to do for you?
     
  8. XP20t

    XP20t

    Joined:
    Jan 4, 2019
    Posts:
    8
    I want to have it something like this:
    instantiate (prefab, spawnPosition1, 0, 0, 0,)

    but then I get an error the same saying No overload for method instantiate takes 5 arguments!
     
  9. XP20t

    XP20t

    Joined:
    Jan 4, 2019
    Posts:
    8
    Thanks for your help now I have my line like this
    Instantiate (Prefab, spawnPosition1, enemy.rotation)