Unity新手求助:PipeSpawnScript出现CS8321警告问题排查
解决Unity中CS8321警告及启动立即生成管道的问题
你的问题出在调用管道生成方法的方式上:
- 你在
Start()和Update()里写的void spawnPipe();是在声明一个局部函数,而不是调用已经定义好的spawnPipe()方法。这就是触发CS8321警告的原因,同时也导致管道根本不会被生成。
修正步骤很简单:
把Start()和Update()里的void spawnPipe();改成直接调用spawnPipe();(去掉前面的void)。这样游戏启动时Start()会立即调用一次生成管道,之后Update()会按设定的spawnRate间隔生成新管道。
修正后的完整代码:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PipeSpawnScript : MonoBehaviour { public GameObject pipe; public float spawnRate = 2; private float timer = 0; // Start is called before the first frame update void Start() { spawnPipe(); } // Update is called once per frame void Update() { if (timer < spawnRate) { timer = timer + Time.deltaTime; } else { spawnPipe(); timer = 0; } } void spawnPipe() { Instantiate(pipe, transform.position, transform.rotation); } }
内容的提问来源于stack exchange,提问作者that_dude
相关产品推荐
相关产品推荐

