if ( kills >= 14) { dialogueBoxs.SetActive(true); } if (sun == 20) { dialogueBox.SetActive(true); }找不到游戏对象时自动weinull
时间: 2023-06-17 07:08:15 浏览: 42
在使用 GameObject.Find() 方法查找游戏对象时,如果场景中不存在该对象,方法会返回 null。因此,在使用 GameObject.Find() 方法之后,应该对返回值进行判断,以避免出现空引用异常。
可以使用以下代码来避免空引用异常:
```
GameObject dialogueBoxs = GameObject.Find("DialogueBoxs");
if (dialogueBoxs != null && kills >= 14) {
dialogueBoxs.SetActive(true);
}
GameObject dialogueBox = GameObject.Find("DialogueBox");
if (dialogueBox != null && sun == 20) {
dialogueBox.SetActive(true);
}
```
这样,即使场景中不存在名为 "DialogueBoxs" 或 "DialogueBox" 的游戏对象,代码也不会出现空引用异常。
相关问题
int kills = protagonist.kill;
这段代码看起来像是从一个对象中获取了一个名为 "protagonist" 的属性 "kill" 的值,然后将其赋值给了一个名为 "kills" 的整型变量。如果 "protagonist" 对象中确实有名为 "kill" 的属性,并且它的值是一个整型数值,那么这段代码应该是有效的。不过如果 "protagonist" 对象不存在或者它没有名为 "kill" 的属性,那么这段代码就会出现编译错误或者运行时错误。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class enemy : MonoBehaviour { public Transform target; float timeTemp; public float speed; public Transform protagonists; public int direction; public int hp; public int kills; public int level; public GameObject bulletPrefab; private kill kill; // Start is called before the first frame update void Start() { kill = GameObject.Find("kill").GetComponent<kill>(); } // Update is called once per frame void Update() { Move(); hpes(); automatic(); } // 控制敌人移动 public void Move() { if (Time.time - timeTemp >= 2) { direction = Random.Range(0, 4); } if (direction == 0) { this.gameObject.transform.Translate(Vector3.up * speed * Time.deltaTime); } else if (direction == 2) { this.gameObject.transform.Translate(Vector3.down * speed * Time.deltaTime); } else if (direction == 3) { this.gameObject.transform.Translate(Vector3.right * speed * Time.deltaTime); } else if (direction == 1) { this.gameObject.transform.Translate(Vector3.left * speed * Time.deltaTime); } } //判断死亡 public void hpes() { if (hp <= 0) { Destroy(this.gameObject); kill.IncreaseKillCount(); } } // 判断是否受伤 public void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.tag == "zhi2") { Destroy(collision.gameObject); hp = hp - 1; } } // 靠近自动攻击 public void automatic() { if (Time.time - timeTemp >= 3) { float distance = Vector3.Distance(transform.position, target.position); Debug.Log(distance); if (distance <= 2) { GameObject Player = GameObject.Find("protagonists"); Vector2 clickPosition = Player.transform.position; GameObject ins = Instantiate(bulletPrefab); ins.GetComponent<Blogs>().clickPosition = clickPosition; if (direction == 0) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y + 0.15f); } else if (direction == 2) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y - 0.15f); } else if (direction == 3) { ins.transform.position = new Vector3(this.gameObject.transform.position.x + 0.15f, this.gameObject.transform.position.y); } else if (direction == 1) { ins.transform.position = new Vector3(this.gameObject.transform.position.x - 0.15f, this.gameObject.transform.position.y); } } timeTemp = Time.time; } } }优化代码
以下是对代码的一些优化建议:
1. 在Start()函数中,应该使用GetComponent<>更具体地获取kill组件,而不是使用Find()函数。
2. 在Move()函数中,可以使用Switch语句替代if else语句,使代码更简洁易读。
3. 在automatic()函数中,可以使用transform.position来获取敌人位置,而不必每次都使用this.gameObject.transform.position。
4. 在automatic()函数中,可以使用transform.up来获取敌人的朝向,从而不必使用direction变量。
5. 在automatic()函数中,可以使用Quaternion.LookRotation()函数来旋转子弹的方向,使其始终面向玩家。
6. 在automatic()函数中,应该使用LayerMask来避免碰撞检测误判,而不是使用tag。
7. 可以使用Object Pooling技术来优化子弹的生成和回收,避免频繁地Instantiate和Destroy。
8. 可以使用协程来实现敌人的移动和攻击动画,使其更加平滑自然。
9. 可以使用状态机来管理敌人的行为,使其更加智能和可控。
10. 可以使用设计模式来优化代码结构,如单例模式、工厂模式、观察者模式等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)