error en unity C#: Error CS0721: ‘Aleatorio’: los tipos estáticos no se pueden usar como parámetros
– UnityAssets3Free
hola , me llamo juansito y para hoy os traigo
nueva pregunta
Encontré un código en Internet que funciona como el de Python random.choices()
:
static class RandomUtils
public static string Choice(this Random rnd, IEnumerable<string> choices, IEnumerable<int> weights)
var cumulativeWeight = new List<int>();
int last = 0;
foreach (var cur in weights)
last += cur;
cumulativeWeight.Add(last);
int choice = rnd.Next(last);
int i = 0;
foreach (var cur in choices)
if (choice < cumulativeWeight[i])
return cur;
i++;
return null;
Añadido a mi código:
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
public class GroundTile : MonoBehaviour
GroundSpawner groundSpawner;
public GameObject player;
private bool doDoubleSpawn;
private int chanceForDoubleSpawn = 45;
void Start()
groundSpawner = GameObject.FindObjectOfType<GroundSpawner>();
player = GameObject.Find("Tractor");
SpawnObstacle();
chanceForDoubleSpawn = GroundSpawner.levelObstaclesMultiplier;
// Spawn weight values for obstacles
private int boxWeightValue = 40;
private int antitankWeightValue = 40;
private int barricadeWeightValue = 40;
private int wheelsWeightValue = 40;
private int molotovItemWeightValue = 5;
// Prefabs
public GameObject obstaclePrefab;
public GameObject boxPrefab;
public GameObject antitankPrefab;
public GameObject barricadePrefab;
public GameObject wheelsPrefab;
public GameObject tankLevel1Prefab;
public GameObject molotovItemPrefab;
// Destroy if player too far away
void Update()
if(transform.position.z < player.transform.position.z - 30)
Destroy(gameObject);
static class RandomUtils
public static string Choice(this Random rnd, IEnumerable<string> choices, IEnumerable<int> weights)
var cumulativeWeight = new List<int>();
int last = 0;
foreach (var cur in weights)
last += cur;
cumulativeWeight.Add(last);
int choice = rnd.Next(last);
int i = 0;
foreach (var cur in choices)
if (choice < cumulativeWeight[i])
return cur;
i++;
return null;
Pero me da error:
AssetsScriptsGroundGeneratorGroundTile.cs(61,30): error CS0721: ‘Random’: los tipos estáticos no se pueden usar como parámetros
¿Cómo puedo corregir eso? ¿Quizás hay otra forma de escribir esta función?
UPD: código que instancia GroundTile:
using UnityEngine;
public class GroundSpawner : MonoBehaviour
{
public GameObject groundTile;
public GameObject groundTileEnd;
public GameObject player;
Vector3 nextSpawnPoint;
private int numOfTiles = 10;
private int plusTiles = 5;
private int count = 0;
public static int level = 1;
public static int levelObstaclesMultiplier = 45;
// Weight values for tanks and items
public static int tankWeightValue = 100;
public static int molotovItemWeightValue = 10;
public static int molotovItemWeightValuePlus = 3;
public void SpawnTile()
GameObject temp = Instantiate(groundTile, nextSpawnPoint, Quaternion.identity);
nextSpawnPoint = temp.transform.GetChild(1).transform.position;
public void SpawnEndTile()
GameObject temp = Instantiate(groundTileEnd, nextSpawnPoint, Quaternion.identity);
nextSpawnPoint = temp.transform.GetChild(1).transform.position;
void Start()
for (int i = 0; i < 10; i++)
SpawnTile();
SpawnEndTile();
InvokeRepeating("Spawn", 1.5f, 1.5f);
public void Spawn()
if (Time.timeScale != 0 && PlayerController.forwardSpeed != 0)
if (count != numOfTiles)
SpawnTile();
count += 1;
else
SpawnEndTile();
numOfTiles += plusTiles;
count = 0;
if (levelObstaclesMultiplier <= 65)
levelObstaclesMultiplier += 1;
}
2 respuestas 2
Este código parece que fue escrito para C# System.Random
en lugar de Unity específico UnityEngine.Random
clase. Puede resolver el error especificando System.Random
en los parámetros:
public static string Choice(this System.Random rnd, IEnumerable<string> choices, IEnumerable<int> weights)
En los parámetros, simplemente reemplace «Random» con «System.Random», el error se debe a que cree que está usando UnityEngine.Random, que es un tipo estático.
public static string Choice(this System.Random rnd, IEnumerable<string> choices, IEnumerable<int> weights)
nota: si aun no se resuelve tu pregunta por favor dejar un comentario y pronto lo podremos de nuevo , muchas gracias
sin mas,espero que te halla servido