【问题标题】:My spawning in script is not working and I am unsure as to why this is happening [closed]我在脚本中的生成不起作用,我不确定为什么会发生这种情况[关闭]
【发布时间】: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;
        }
    }
}

【问题讨论】:

  • 看起来AlkGoblinSpawnGoblin 中的一个或多个在检查器中未分配。
  • 我已经在检查器中分配了它们我可以显示截图@Ruzihm
  • 是的,这是有道理的,否则在 Start 中会出现 nullreferenceexception。我无法从问题中的信息中重现此问题 - 请包含minimal reproducible example。对于统一问题,这包括从新项目开始重新创建问题的步骤,包括如何设置场景、附加组件等。请参阅this question 以获得一个很好的示例。另一种方法是描述您当前案例中正在发生的更多事情 - 是否有其他活跃的单一行为?删除后问题是否停止?等
  • @Ruzihm 我添加了更多编辑以帮助缩小答案范围,但据我所知,没有其他单一行为干扰此
  • 我建议尝试创建一个新场景并逐步重现此问题并查看它何时再次出现。我唯一的猜测是可能存在另一个未正确配置的 SpawnInScript 实例。没有minimal reproducible example,真的没什么好说的了。同样令人非常失望的是,在提到它的可能性之前,由于某种原因没有提到另一个 NRE。

标签: c# unity3d nullreferenceexception


【解决方案1】:

我意识到,当敌人在区域内生成时,值不会回升,因为它们生成时没有任何增加值。我还对代码进行了更多优化。

我可以通过这样做来修复它:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnInScript : MonoBehaviour
{
    [System.Serializable]
    public class EnemyWorth
    {        
        public int weight;
        public GameObject self;

        public EnemyWorth(int weight, GameObject self)
        {            
            this.weight = weight;
            this.self = self;            
        }
    }

    public GameObject[] monsters;

    public EnemyWorth[] enemies = new EnemyWorth[6];

    public int maxAreaWorth;

    public float dimX;
    public float dimY;
    public float dimZ;


    [SerializeField]
    private int areaWorth;

    private int rand;

    private float randX;
    private float randY;
    private float randZ;

    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < monsters.Length; i++)
        {
            enemies[i] = new EnemyWorth(monsters[i].GetComponent<EnemyBaseScript>().worth, monsters[i]);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (areaWorth < maxAreaWorth)
        {
            rand = Random.Range(0, 3);
            randX = Random.Range(-dimX, dimX);
            randY = Random.Range(-dimY, dimY);
            randZ = Random.Range(-dimZ, dimZ);

            Instantiate(enemies[rand].self, new Vector3(this.transform.position.x -randX, this.transform.position.y - randY, this.transform.position.z - randZ), Quaternion.identity);
            areaWorth += enemies[rand].weight;
        }
    }


    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;
        }
    }
}


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 2021-01-24
    相关资源
    最近更新 更多