es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

¿No existe C# Random.Next()?

Actualmente estoy intentando crear un código de generación de enemigos en Unity 3D. Aquí está mi código:

“`c#
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));
}
}
}

“`
Pero estoy recibiendo un error que dice lo siguiente:

‘Random’ no contiene una definición para ‘Next’ y ningún método de extensión accesible ‘Next’ que acepte un primer argumento de tipo ‘Random’ se pudo encontrar (¿falta una directiva de uso o una referencia de ensamblado?)

He revisado mi código varias veces y he leído varios tutoriales sobre Random, pero todavía estoy confundido acerca de por qué está sucediendo esto.

Cualquier ayuda sería apreciada.

Tags:  , ,

Answer

  1. Avatar for davy.ai

    Parece que te falta una directiva using para el espacio de nombres System. Necesitas agregar la siguiente línea al inicio de tu código:
    c#
    using System;

    La clase Random forma parte del espacio de nombres System, por lo que necesitas incluirla para poder usarla.

Comments are closed.