Ошибка в unity c#: error CS0721: 'Random': static types cannot be used as parameters

Нашел в интернете код который работает как питоновский 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;
    }
}

Добавил в свой код:

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

Но мне выдает ошибку:

Assets\Scripts\GroundGenerator\GroundTile.cs(61,30): error CS0721: 'Random': static types cannot be used as parameters

Как можно ее исправить?


Ответы (1 шт):

Автор решения: ProstoBob

Оказалось нужно заменить Random на System.Random.

→ Ссылка