Unity实例化错误
I've created a script that should spawn enemies in some time, and it works perfectly. However, for some reason it shows errors, even then it spawns enemies like it should.
代码:
public class Spawner : MonoBehaviour
{
[SerializeField]
private GameObject[] meteors;
public GameObject meteor1;
private float TimeBetweenSpawns;
public float StartTimeBetweenSpawn = 1f;
public float MaxY = 4.0f;
public float MinY = -4.0f;
float yPos;
float MinTime = 1f;
float MaxTime = 5f;
private Vector3 spawnPos;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//ChangeSpawnTimes();
yPos = Random.Range(MinY, MaxY);
spawnPos = new Vector3(transform.position.x, yPos, transform.position.z);
int WhichMeteor = 0;
int WhichMeteorRandom = Random.Range(1, 10);
if(WhichMeteorRandom > 8)
{
WhichMeteor = 0;
}
else
{
WhichMeteor = 1;
}
Debug.Log(WhichMeteor.ToString());
if (TimeBetweenSpawns <= 0)
{
Instantiate(meteors[0], transform.position, transform.rotation);
//float Time = Random.Range(1f,5f);
TimeBetweenSpawns = 1f;
}
else
{
TimeBetweenSpawns -= Time.deltaTime;
}
}
private void ChangeSpawnTimes()
{
Debug.Log(Points.points);
}
}
如果我尝试仅添加1个Metor而不是数组,只需创建gameObject
再次向Inspector添加prefab再次显示出错误,但可以按照原样的速度工作,即它及时催生敌人。
Instantiate(meteor1, transform.position, transform.rotation);
我的错误在哪里?
I’ve created a script that should spawn enemies in some time, and it works perfectly. However, for some reason it shows errors, even then it spawns enemies like it should.
Code:
public class Spawner : MonoBehaviour
{
[SerializeField]
private GameObject[] meteors;
public GameObject meteor1;
private float TimeBetweenSpawns;
public float StartTimeBetweenSpawn = 1f;
public float MaxY = 4.0f;
public float MinY = -4.0f;
float yPos;
float MinTime = 1f;
float MaxTime = 5f;
private Vector3 spawnPos;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//ChangeSpawnTimes();
yPos = Random.Range(MinY, MaxY);
spawnPos = new Vector3(transform.position.x, yPos, transform.position.z);
int WhichMeteor = 0;
int WhichMeteorRandom = Random.Range(1, 10);
if(WhichMeteorRandom > 8)
{
WhichMeteor = 0;
}
else
{
WhichMeteor = 1;
}
Debug.Log(WhichMeteor.ToString());
if (TimeBetweenSpawns <= 0)
{
Instantiate(meteors[0], transform.position, transform.rotation);
//float Time = Random.Range(1f,5f);
TimeBetweenSpawns = 1f;
}
else
{
TimeBetweenSpawns -= Time.deltaTime;
}
}
private void ChangeSpawnTimes()
{
Debug.Log(Points.points);
}
}
If I try to add only 1 metor not an array, just creating GameObject
adding prefab to an inspector it again shows an error, but works as it should, i.e. it spawns enemies in a timely manner.
Instantiate(meteor1, transform.position, transform.rotation);
Where is my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您的代码中有一个数组&amp;您正在尝试访问其不存在的索引之一。
例如:
错误是因为
r [2]
为null&amp;未定义。因此,解决方案是在尝试访问该索引之前检查索引是否未零。Seems that you have an array in your code & you're trying to access one of its indexes that don't exist.
for example:
The error is because
r[2]
is null & isn't defined. So the solution is to check if the index is not null before trying to access it.