问题描述
我在无限跑酷游戏中工作,我想在平台上添加对象池,但出现2个错误:
Assets\PlatformGenerator.cs(37,26): error CS1501: No overload for method 'SpawnObject' takes 1 arguments
Assets\ObjectPool.cs(36,16): error CS0165: Use of unassigned local variable 'ToReturn'
这是平台生成器脚本:
{
public GameObject ThePlatform;
public Transform GenerationPoint;
public float distanceBetween;
private float PlatFormWidth;
public float distanceBetweenMin;
public float distanceBetweenMax;
public ObjectPool PlatformPool;
// Start is called before the first frame update
void Start()
{
PlatFormWidth = ThePlatform.GetComponent<BoxCollider2D>().size.x;
}
// Update is called once per frame
void Update()
{
if(transform.position.x < GenerationPoint.position.x)
{
distanceBetween = Random.Range(distanceBetweenMin,distanceBetweenMax);
transform.position = new Vector3(transform.position.x + PlatFormWidth + distanceBetween,transform.position.y,transform.position.z);
//Instantiate(ThePlatform,transform.position,transform.rotation);
PlatformPool.SpawnObject(ThePlatform,transform.rotation);
}
}
}
这是ObjectPooler脚本:
{
public GameObject ObjectToPool;
public List<GameObject> ThePool = new List<GameObject>();
public int StartAmount;
// Start is called before the first frame update
void Start()
{
for(int i = 0; i < StartAmount; i++)
{
ThePool.Add(Instantiate(ObjectToPool));
ThePool[i].SetActive(false);
ThePool[i].transform.parent = transform;
}
}
// Update is called once per frame
void Update()
{
}
public GameObject SpawnObject(Vector3 Position)
{
GameObject ToReturn;
ToReturn = ThePool[0];
ThePool.RemoveAt(0);
ToReturn.transform.position = Position;
ToReturn.SetActive(true);
return ToReturn;
}
}
希望您能对我有所帮助,因为我是编程人员中的新手,现在不要对此类问题进行热修复,如果您看到其他问题,我应该加以改进,如果您告诉我,这将非常好。
解决方法
在Object Pooler脚本中,放置以下函数public GameObject SpawnObject(Vector3 Position),该函数仅使用一个位置参数。但是在平台生成器中,您正在使用三个参数PlatformPool.SpawnObject(ThePlatform,transform.position,transform.rotation)调用函数。因此,将平台生成器脚本中的PlatformPool.SpawnObject(ThePlatform,transform.position,transform.rotation)替换为PlatformPool.SpawnObject(transform.position);。
,第一个错误,您的方法仅要求一个参数。您正在传递3。(Vector3仅表示变换或gameObject的x,y,z点。)如果要动态传递Vector3对象,则可以执行以下操作
Vector3 v = new Vector3 (0,0);
然后将其传递给方法SpawnObject(v);
对于第二个错误,请确保ThePool列表不为空并且您正在正确分配它们,因为您已将其分配给ToReturn。
记录有很大帮助,请尝试调试类似的东西
Debug.LogError(ThePool == null);
或检查其长度
Debug.LogError(ThePool.Length);
如果是这样,请尝试添加条件,以免出现错误
if(ToReturn != null)
// ....