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);
}
}
}
下面是根据你代码的测试代码,我测试没有问题。
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);
}
}
}