代码之家  ›  专栏  ›  技术社区  ›  Andrei Vechar

unity c#中出错:错误CS0721:“随机”:静态类型不能用作参数

  •  1
  • Andrei Vechar  · 技术社区  · 4 年前

    我在网上找到了一个类似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;
        }
    }
    

    添加到我的代码中:

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

    但这给了我一个错误:

    资产\Scripts\GroundGenerator\GroundTile。cs(61,30):错误CS0721:“随机”:静态类型不能用作参数

    我该怎么解决这个问题?也许还有其他方法来编写这个函数?

    UPD:实例化地砖的代码:

    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 回复  |  直到 4 年前
        1
  •  1
  •   Sven Viking    4 年前

    这段代码似乎是为C#编写的 System.Random 而不是具体的统一 UnityEngine.Random 班您可以通过指定 系统随机的 在参数中:

    public static string Choice(this System.Random rnd, IEnumerable<string> choices, IEnumerable<int> weights)
    
        2
  •  1
  •   Houtamelo    4 年前

    在参数上,只需将“Random”替换为“System.Random”,错误是因为它认为您正在使用UnityEngine。随机,这是一种静态类型。

    public static string Choice(this System.Random rnd, IEnumerable<string> choices, IEnumerable<int> weights)