【发布时间】:2022-01-15 06:46:52
【问题描述】:
我在脚本中的生成应该如何工作是有一个大立方体(250 x 250 x 250),并且它有一个启用触发器的盒子对撞机。每个暴徒都有一个值,即他们的生命值/10。我的目标是让每个区域的值都为 100,如果小于该值,它将随机生成一个新的暴民,直到它回到 100 值。我在实例化暴民的行上收到一个错误,它给出了一个空引用异常错误。我已经在检查器中分配了敌人的游戏对象。我故意不在蜘蛛中产卵,因为我正在为它们做一些特别的事情。如果有任何代码你只需要评论,我应该可以给你。谢谢
编辑:在我将 Alk 添加到 Enemies 列表的那一行开始时,我也遇到了一个空引用异常错误
编辑:在这个场景中,没有其他对象会干扰生成,因为我一一禁用了所有其他对象并且我没有出错。与此相关的敌人基础脚本中的所有值都有已分配给它们的值。我希望这有助于缩小范围
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnInScript : MonoBehaviour
{
public class EnemyWorth
{
public float health;
public int weight;
public GameObject self;
public EnemyWorth(float health, int weight, GameObject self)
{
this.health = health;
this.weight = weight;
this.self = self;
}
}
public GameObject Alk;
public GameObject GoblinSpawn;
public GameObject Goblin;
public GameObject Cobra;
public GameObject Spider;
public GameObject SpiderMini;
[SerializeField]
public EnemyWorth[] Enemies;
public int areaWorth;
private int rand;
private float randX;
private float randY;
private float randZ;
// Start is called before the first frame update
void Start()
{
Enemies[0] = new EnemyWorth (Alk.GetComponent<EnemyBaseScript>().health, Alk.GetComponent<EnemyBaseScript>().worth, Alk);
Enemies[1] = new EnemyWorth (GoblinSpawn.GetComponent<EnemyBaseScript>().health, GoblinSpawn.GetComponent<EnemyBaseScript>().worth, GoblinSpawn);
Enemies[2] = new EnemyWorth (Goblin.GetComponent<EnemyBaseScript>().health, Goblin.GetComponent<EnemyBaseScript>().worth, Goblin);
Enemies[3] = new EnemyWorth (Cobra.GetComponent<EnemyBaseScript>().health, Cobra.GetComponent<EnemyBaseScript>().worth, Cobra);
Enemies[4] = new EnemyWorth (Spider.GetComponent<EnemyBaseScript>().health, Spider.GetComponent<EnemyBaseScript>().worth, Spider);
Enemies[5] = new EnemyWorth (SpiderMini.GetComponent<EnemyBaseScript>().health, SpiderMini.GetComponent<EnemyBaseScript>().worth, SpiderMini);
}
// Update is called once per frame
void Update()
{
if (areaWorth > 100)
{
return;
}
if (areaWorth < 100)
{
rand = Random.Range(0, 3);
randX = Random.Range(-125, 125);
randY = Random.Range(-125, 125);
randZ = Random.Range(-125, 125);
Instantiate(Enemies[rand].self, new Vector3(this.transform.position.x -randX, this.transform.position.y - randY, this.transform.position.z - randZ), Quaternion.identity); // I am getting an error on this line
}
}
private void OnTriggerEnter(Collider col)
{
if (col.gameObject == Enemies[0].self)
{
areaWorth += Enemies[0].weight;
}
else if (col.gameObject == Enemies[1].self)
{
areaWorth += Enemies[1].weight;
}
else if (col.gameObject == Enemies[2].self)
{
areaWorth += Enemies[2].weight;
}
else if (col.gameObject == Enemies[3].self)
{
areaWorth += Enemies[3].weight;
}
else if (col.gameObject == Enemies[4].self)
{
areaWorth += Enemies[4].weight;
}
else if (col.gameObject == Enemies[5].self)
{
areaWorth += Enemies[5].weight;
}
}
private void OnTriggerExit(Collider col)
{
if (col.gameObject == Enemies[0].self)
{
areaWorth += Enemies[0].weight;
}
else if (col.gameObject == Enemies[1].self)
{
areaWorth += Enemies[1].weight;
}
else if (col.gameObject == Enemies[2].self)
{
areaWorth += Enemies[2].weight;
}
else if (col.gameObject == Enemies[3].self)
{
areaWorth += Enemies[3].weight;
}
else if (col.gameObject == Enemies[4].self)
{
areaWorth += Enemies[4].weight;
}
else if (col.gameObject == Enemies[5].self)
{
areaWorth += Enemies[5].weight;
}
}
}
【问题讨论】:
-
看起来
Alk、GoblinSpawn或Goblin中的一个或多个在检查器中未分配。 -
我已经在检查器中分配了它们我可以显示截图@Ruzihm
-
是的,这是有道理的,否则在 Start 中会出现 nullreferenceexception。我无法从问题中的信息中重现此问题 - 请包含minimal reproducible example。对于统一问题,这包括从新项目开始重新创建问题的步骤,包括如何设置场景、附加组件等。请参阅this question 以获得一个很好的示例。另一种方法是描述您当前案例中正在发生的更多事情 - 是否有其他活跃的单一行为?删除后问题是否停止?等
-
@Ruzihm 我添加了更多编辑以帮助缩小答案范围,但据我所知,没有其他单一行为干扰此
-
我建议尝试创建一个新场景并逐步重现此问题并查看它何时再次出现。我唯一的猜测是可能存在另一个未正确配置的 SpawnInScript 实例。没有minimal reproducible example,真的没什么好说的了。同样令人非常失望的是,在提到它的可能性之前,由于某种原因没有提到另一个 NRE。
标签: c# unity3d nullreferenceexception