Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Error CS0104 Random.Range()

Discussion in 'Scripting' started by Brother_Games, Jul 28, 2020.

  1. Brother_Games

    Brother_Games

    Joined:
    Jul 28, 2020
    Posts:
    11
    Hello guys,
    I am new to Unity and learn with some tutorials from YouTube but now I got the error CS0104 and I dont know what I should do. The error is in every line with "Random.Range" and said:

    '"Random" ist ein mehrdeutiger Verweis zwischen "UnityEngine.Random" und "System.Random". Assembly-CSharp C:\Users\phili\Unity Projekte\Jumpy Runner\Assets\scripts\obstacle_spawnen.cs

    Here is my skript I hope you can help me

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using System.Security.Cryptography;
    6. using System.Threading;
    7. using UnityEngine;
    8.  
    9. public class obstacle_spawnen : MonoBehaviour
    10. {
    11. public GameObject[] obstacles;
    12. public List<GameObject> obstaclesToSpawn = new List<GameObject>();
    13.  
    14. int index;
    15.  
    16. void Awake()
    17. {
    18. InitObstacles();
    19. }
    20.  
    21. void Start()
    22. {
    23. StartCoroutine(SpawnObstacles());
    24. }
    25.  
    26. void InitObstacles()
    27. {
    28. index = 0;
    29. for (int i = 0; i < obstacles.Length; i++)
    30. {
    31. GameObject obj = Instantiate(obstacles[index], transform.position, Quaternion.identity);
    32. obstaclesToSpawn.Add(obj);
    33. obstaclesToSpawn.SetActive(false);
    34. index++;
    35.  
    36. if (index == obstacles.Length)
    37. {
    38. index = 0;
    39. }
    40. }
    41. }
    42. IEnumerator SpawnObstacles()
    43. {
    44. yield return new WaitForSeconds(Random.Range(1.5f, 4.5f));
    45. int index = Random.Range(0, obstaclesToSpawn);
    46.  
    47. while (true)
    48. {
    49. if (!obstaclesToSpawn[index].activeInHierarchy)
    50. {
    51. obstaclesToSpawn[index].SetActive(true);
    52. break;
    53. }
    54. else
    55. {
    56. index = Random.Range(0, obstaclesToSpawn);
    57. }
    58. }
    59. StartCoroutine(SpawnObstacles());
    60. }
    61. }
     
    Last edited: Jul 28, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    8,314
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,941
    For each instance of "Random", type "UnityEngine.Random", e.g. "UnityEngine.Random.Range(....." That will make the reference not ambiguous between the two possible "Random" classes.
     
  4. Brother_Games

    Brother_Games

    Joined:
    Jul 28, 2020
    Posts:
    11
    Thanks for the clue but I tried this before and than come the error CS1503

    Argument "2": Konvertierung von "System.Collections.Generic.List<UnityEngine.GameObject>" in "float" nicht möglich. Assembly-CSharp C:\Users\phili\Unity Projekte\Jumpy Runner\Assets\scripts\obstacle_spawnen.cs

    for the last two Random.Range() ´s. Maybe you can help me of this too.
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using System.Security.Cryptography;
    6. using System.Threading;
    7. using UnityEngine;
    8.  
    9. public class obstacle_spawnen : MonoBehaviour
    10. {
    11.     public GameObject[] obstacles;
    12.     public List<GameObject> obstaclesToSpawn = new List<GameObject>();
    13.  
    14.     int index;
    15.  
    16.     void Awake()
    17.     {
    18.         InitObstacles();
    19.     }
    20.  
    21.     void Start()
    22.     {
    23.         StartCoroutine(SpawnObstacles());
    24.     }
    25.  
    26.     void InitObstacles()
    27.     {
    28.         index = 0;
    29.         for (int i = 0; i < obstacles.Length; i++)
    30.         {
    31.             GameObject obj = Instantiate(obstacles[index], transform.position, Quaternion.identity);
    32.             obstaclesToSpawn.Add(obj);
    33.             obstaclesToSpawn[i].SetActive(false);
    34.             index++;
    35.  
    36.             if (index == obstacles.Length)
    37.             {
    38.                 index = 0;
    39.             }
    40.         }
    41.     }
    42.     IEnumerator SpawnObstacles()
    43.     {
    44.         yield return new WaitForSeconds(UnityEngine.Random.Range(1.5f, 4.5f));
    45.         int index = UnityEngine.Random.Range(0, obstaclesToSpawn);
    46.  
    47.         while (true)
    48.         {
    49.             if (!obstaclesToSpawn[index].activeInHierarchy)
    50.             {
    51.                 obstaclesToSpawn[index].SetActive(true);
    52.                 break;
    53.             }
    54.             else
    55.             {
    56.                 index = UnityEngine.Random.Range(0, obstaclesToSpawn);
    57.             }
    58.         }
    59.         StartCoroutine(SpawnObstacles());
    60.     }
    61. }
     
  5. DaDonik

    DaDonik

    Joined:
    Jun 17, 2013
    Posts:
    258
    index = UnityEngine.Random.Range(0, obstaclesToSpawn);

    should be
    index = UnityEngine.Random.Range(0, obstaclesToSpawn.Count);

    because you want the number of elements in the list, not the list itself.
     
    Bunny83 likes this.
  6. Brother_Games

    Brother_Games

    Joined:
    Jul 28, 2020
    Posts:
    11
    Thanks you helped mi so much it was a problem for me hole 3 days thanks!
     
unityunity