jeudi 18 novembre 2021

C# Random.Next() Does not Exist?

I am attempting to make some enemy spawning code at the moment in Unity 3D. Here is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class GameControllerScript : MonoBehaviour
    {
        public float amountKilled = 0f;
        private float timer = 5f;
        private float startTime = 5f;
        public GameObject EnemyPrefab;
        private readonly Random rnd = new Random();
        void Update()
        {
            timer -= Time.deltaTime;
            if (timer <= 0)
            {
                timer = startTime - 0.1f;
                startTime -= 0.1f;
                int spawnNum = rnd.Next(1, 5);
                Instantiate(EnemyPrefab, GameObject.Find("SpawnPoint" + spawnNum.ToString()).GetComponent<Transform>().position, new Quaternion(0,0,0,0));
            }
        }
    }

But I am receiving an error that says the following:

'Random' does not contain a definition for 'Next' and no accessible extension method 'Next' accepting a first argument of type 'Random' could be found (are you missing a using directive or an assembly reference?)

I have double-checked my code multiple times and read multiple tutorials about Random but am still confused about why this is happening.

Any help would be appreciated.




Aucun commentaire:

Enregistrer un commentaire