System.Random errors.

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);

}
}

Delete the line

using System.Random;

When you are using Random.Range you are using the namespace UnityEngine and not System.Random

But even when I am using UnityEngine.Random it gives me the same error.

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 ()
* *private void Update() { if(Input.GetKeyDown(KeyCode.T)) { SpawnEnemy(); }* *
The second error you got is on the line :

* *spawners = transform.GetChild(i).gameObject;* *
and should be :
* *spawners[i] = transform.GetChild(i).gameObject;* *

This is your script working now :

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using UnityEngine;



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[i] = 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 Likes