Search Unity

System.Random errors.

Discussion in 'Scripting' started by AlexNik09, May 6, 2020.

  1. AlexNik09

    AlexNik09

    Joined:
    Sep 30, 2019
    Posts:
    2
    So I am currently making an enemy random spawn system but I still can't understand the errors that I am getting.
    The Error: error CS0138: A 'using namespace' directive can only be applied to namespaces; 'Random' is a type not a namespace. Consider a 'using static' directive instead

    I have tried my best to google it and solve it myself but I still can't understand why it's not working.
    Any help will be much appreciated.
    And Visual Studio isn't giving me any errors, by the way, it's all in unity.

    The Code:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel.Design;
    using UnityEngine;
    using System.Random;



    public class EnemySpawner : MonoBehaviour
    {
    public GameObject[] spawners;
    public GameObject enemy;


    private void Start()
    {
    spawners = new GameObject[5];

    for(int i = 0; i < spawners.Length; i++)
    {
    spawners = transform.GetChild(i).gameObject;
    }

    }

    private void Update()
    {
    if(Input.GetKeyDown(KeyCode.T))
    {
    SpawnEnemy;
    }


    }



    private void SpawnEnemy()
    {
    int spawnerID = Random.Range(0, spawners.Length);
    Instantiate(enemy, spawners[spawnerID].transform.position, spawners[spawnerID].transform.rotation);



    }


    }
     
  2. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    Delete the line

    Code (csharp):
    1.  
    2. using System.Random;
    3.  
    When you are using Random.Range you are using the namespace UnityEngine and not System.Random
     
  3. AlexNik09

    AlexNik09

    Joined:
    Sep 30, 2019
    Posts:
    2
    But even when I am using UnityEngine.Random it gives me the same error.
     
  4. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    Don't use also System don't make using System

    Here is your code working with two other fixed errors :

    Inside Update you called the function SpawnEnemy and forgot to add ()

    Code (csharp):
    1.  
    2. private void Update()
    3. {
    4. if(Input.GetKeyDown(KeyCode.T))
    5. {
    6. SpawnEnemy();
    7. }
    8.  
    The second error you got is on the line :


    Code (csharp):
    1.  
    2. spawners = transform.GetChild(i).gameObject;
    3.  
    and should be :

    Code (csharp):
    1.  
    2. spawners[i] = transform.GetChild(i).gameObject;
    3.  


    This is your script working now :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.ComponentModel.Design;
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class EnemySpawner : MonoBehaviour
    10. {
    11.     public GameObject[] spawners;
    12.     public GameObject enemy;
    13.  
    14.     private void Start()
    15.     {
    16.         spawners = new GameObject[5];
    17.  
    18.         for (int i = 0; i < spawners.Length; i++)
    19.         {
    20.             spawners[i] = transform.GetChild(i).gameObject;
    21.         }
    22.  
    23.     }
    24.  
    25.     private void Update()
    26.     {
    27.         if (Input.GetKeyDown(KeyCode.T))
    28.         {
    29.             SpawnEnemy();
    30.         }
    31.     }
    32.  
    33.     private void SpawnEnemy()
    34.     {
    35.         int spawnerID = Random.Range(0, spawners.Length);
    36.         Instantiate(enemy, spawners[spawnerID].transform.position, spawners[spawnerID].transform.rotation);
    37.     }
    38. }
    39.  
     
    metaldc4life and PraetorBlue like this.