我在脚本中的生成不起作用,我不确定为什么会这样
My spawning in script is not working and I am unsure as to why this is happening
我在脚本中的生成应该如何工作是有一个大立方体(250 x 250 x 250)并且它有一个启用了触发器的盒子碰撞器。每个生物都有一个值,即它们的 health/10。我的目标是让每个区域的值都为 100,如果低于该值,它将随机生成一个新的生物,直到它回到 100 值。我在实例化 mob 的那一行出现错误,它给出了一个空引用异常错误。我已经在 instpector 中分配了敌方游戏对象。我有意不在蜘蛛中产卵,因为我正在为它们做一些特别的事情。如果有任何代码你只需要评论,我应该可以给你。谢谢
编辑:在我将 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;
}
}
}
我意识到当敌人在该区域中生成时,该值不会回升,因为当它们生成时没有任何东西增加该值。我还进一步优化了代码。
我可以通过这样做来修复它:
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;
}
}
}
我在脚本中的生成应该如何工作是有一个大立方体(250 x 250 x 250)并且它有一个启用了触发器的盒子碰撞器。每个生物都有一个值,即它们的 health/10。我的目标是让每个区域的值都为 100,如果低于该值,它将随机生成一个新的生物,直到它回到 100 值。我在实例化 mob 的那一行出现错误,它给出了一个空引用异常错误。我已经在 instpector 中分配了敌方游戏对象。我有意不在蜘蛛中产卵,因为我正在为它们做一些特别的事情。如果有任何代码你只需要评论,我应该可以给你。谢谢
编辑:在我将 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;
}
}
}
我意识到当敌人在该区域中生成时,该值不会回升,因为当它们生成时没有任何东西增加该值。我还进一步优化了代码。
我可以通过这样做来修复它:
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;
}
}
}