我即方位,我即吉凶 2022-05-28 17:05 采纳率: 100%
浏览 23
已结题

物体序列在游戏中不被创建

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AppleMgr : MonoBehaviour
{
public class AppleCreator
{
private static GameObject[] s_appleListPrefab = new GameObject[2];
public static void RandomCreate(Vector3 startPos)
{
var id = Random.Range(0, 2);
if (null == s_appleListPrefab[id])
{

            s_appleListPrefab[id] = Resources.Load<GameObject>("AppleList" + id);
        }
        var appleListRoot = Object.Instantiate(s_appleListPrefab[id]);
        var appleBhv = appleListRoot.AddComponent<Apple>();

        appleBhv.SetPos(startPos);
    }
}

}

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 小赵爱吃肉 2022-05-28 17:49
    关注

    下面是根据你代码的测试代码,我测试没有问题。

    
    using UnityEngine;
    
    public class AppleMgr : MonoBehaviour
    {
        public Transform[] trf;
        public class AppleCreator
        {
            private static GameObject[] s_appleListPrefab = new GameObject[2];
            public static void RandomCreate(Vector3 startPos)
            {
                var id = Random.Range(0, 2);
                if (null == s_appleListPrefab[id])
                {
    
                    s_appleListPrefab[id] = Resources.Load<GameObject>("AppleList" + id);
                }
                var appleListRoot = Object.Instantiate(s_appleListPrefab[id]);
                //var appleBhv = appleListRoot.AddComponent<Apple>();
                appleListRoot.transform.position = startPos;
                //appleBhv.SetPos(startPos);
            }
        }
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.P))
            { int inttger = Random.Range(0, trf.Length);
                AppleCreator.RandomCreate(trf[inttger].position);
            }
        }
    }
    

    img

    本回答被专家选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月9日
  • 专家已采纳回答 6月1日
  • 创建了问题 5月28日