【问题标题】:Stuck with comparing an random generated string with game object name/tag?无法将随机生成的字符串与游戏对象名称/标签进行比较?
【发布时间】:2020-08-05 16:29:35
【问题描述】:

开发一个小游戏,会在文本空间中生成随机名称,并且玩家应该与文本空间中显示的名称的对象发生碰撞。例如,如果显示“柠檬”,那么玩家应该触摸放在桌子上的柠檬游戏对象。

   using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class RandomString : MonoBehaviour
    {
        [SerializeField] Text randName;
        public GameObject[] veggies;
        string[] veg = { "Olive", "Lemon", "Carrot" };
    
        void Start()
        {
            randName.text = veg[1];
        }
    
        void Update()
        {
            GenerateRandom(3);
    
        }
        private void GenerateRandom(int maxInt)
        {
            int rnd = UnityEngine.Random.Range(0, maxInt);
            if (Input.GetKey(KeyCode.Space))
            {
                string a =  veg[rnd];
                randName.text = a;
    
            }
        }
        void OnCollisionEnter(Collision col, string a)
        {
            if (col.gameObject.CompareTag(a))
            {
                Debug.Log("Hit");
            }
        }
    
    }

【问题讨论】:

    标签: c# unity3d game-development


    【解决方案1】:

    为什么在if(Input.GetKey) 块内只需要一个随机值,却不断生成一个新的随机值...

    还有:你真的想在按钮按下的每一帧都分配一个新的随机数吗?第一次还不够吗(GetKeyDown)?

    然后你不能只更改OnCollisionEnter(Collider)的签名,否则Unity无法识别此消息方法并且不会调用。

    我觉得应该是

    // Store the random string in class scope
    string a;
    
    private void Start()
    {
        GenerateRandom ();
    }
    
    private void Update()
    {
        // While debugging I assume you will use this
        // otherwise there is no need later to call the method continuously
        // Only generate one new random for each press of Space
        if (Input.GetKeyDown(KeyCode.Space))
        {
            GenerateRandom();
        }
    }
    
    public void GenerateRandom()
    {
        // You only need a random index if you are actually gonna use it
        // Note I would get rid of the additional tags array and rather 
        // directly use the tags of given objects!
        // This way you can't mess them up and can easily add more
        int rnd = UnityEngine.Random.Range(0, veggies.Length);
        a =  veggies[rnd].tag;
        randName.text = a;
    }
    
    void OnCollisionEnter(Collision col)
    {
        // Use the "a" stored in class scope not a parameter
        if (col.gameObject.CompareTag(a))
        {
            Debug.Log("Hit");
    
            // Little addition from my side
            // You could probably directly generate the next random after a match
            // so you wouldn't need the button and Update at all
            GenerateRandom();
        }
    }
    

    您实际上也可以直接使用对象引用及其名称,而不是使用标签:

    GameObject a;
    
    private void GenerateRandom()
    {
        int rnd = UnityEngine.Random.Range(0, veggies.Length);
        a =  veggies[rnd];
        randName.text = a.name;
    }
    
    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject == a)
        {
            Debug.Log("Hit");
    
            GenerateRandom();
        }
    }
    

    【讨论】:

    • 是否可以在没有更新功能的情况下更改值?只有在我按一次空格键后才会发生碰撞
    • @user3437968 是的,是的!在Start 中调用GenerateRandom 一次;)
    • 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2011-09-01
    • 2023-03-23
    • 2013-07-28
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多