From 5f2c738dd80c5abcb2a54e9a303e30553322e3e4 Mon Sep 17 00:00:00 2001 From: chomn <2693781894@qq.com> Date: Mon, 11 Mar 2024 17:36:22 +0800 Subject: [PATCH] there are so many exceptions when run the game,check the null reference to avoid them --- .gitignore | 3 +++ Assets/Scripts/BulletScript.cs | 5 +++- Assets/Scripts/CannonScript.cs | 8 ++++++- Assets/Scripts/CoinScript.cs | 6 +++-- Assets/Scripts/HealthDrawerScript.cs | 7 ++++-- Assets/Scripts/PathFollower.cs | 36 +++++++++++++++++++--------- Assets/Scripts/Pool.cs | 5 +++- Assets/Scripts/RocketScript.cs | 9 ++++--- Assets/Scripts/StartButtonScript.cs | 8 +++++-- 9 files changed, 64 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 44e6a3c..be1a35d 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ sysinfo.txt *.apk *.unitypackage build_windows.zip +.idea +/Assets/Plugins/Editor/JetBrains +/Assets/Plugins/Editor/JetBrains.meta diff --git a/Assets/Scripts/BulletScript.cs b/Assets/Scripts/BulletScript.cs index dedb3ef..0f19771 100644 --- a/Assets/Scripts/BulletScript.cs +++ b/Assets/Scripts/BulletScript.cs @@ -13,7 +13,10 @@ void Start() void OnEnable () { distance = 0.0f; - Pool.Instance.ActivateObject("rifleSoundEffect").SetActive(true); + //before active the activateObject, check the null reference situation + GameObject activateObject = Pool.Instance.ActivateObject("rifleSoundEffect"); + if(activateObject != null) + activateObject.SetActive(true); } // Update is called once per frame diff --git a/Assets/Scripts/CannonScript.cs b/Assets/Scripts/CannonScript.cs index b9c79ab..9b918f8 100644 --- a/Assets/Scripts/CannonScript.cs +++ b/Assets/Scripts/CannonScript.cs @@ -21,10 +21,16 @@ void Start () { enemyTags = Enemies.Select(e => e.tag).ToList(); var enemy = EnemyManagerScript.Instance.GetEnemyInRange(transform.position, float.PositiveInfinity, enemyTags); + //the enemy might be null,if it is null ,return here + if(enemy == null) + return; var angle = MathHelpers.Angle(enemy.transform.position - transform.position, transform.up); transform.eulerAngles = new Vector3(0, 0, angle); - bulletPlaceholder = transform.Find("Rocket").gameObject; + //Rocket might be not found ,null reference check + Transform rocket = transform.Find("Rocket"); + if(rocket != null) + bulletPlaceholder = rocket.gameObject; } void FixedUpdate () diff --git a/Assets/Scripts/CoinScript.cs b/Assets/Scripts/CoinScript.cs index efaf39e..0060a35 100644 --- a/Assets/Scripts/CoinScript.cs +++ b/Assets/Scripts/CoinScript.cs @@ -24,7 +24,9 @@ void OnMouseOver() { GameManager.Instance.CoinCollected(gameObject); Pool.Instance.DeactivateObject(gameObject); - - Pool.Instance.ActivateObject("coinSoundEffect").SetActive(true); + //null reference check + GameObject coinSoundEffect = Pool.Instance.ActivateObject("coinSoundEffect"); + if (coinSoundEffect != null) + coinSoundEffect.SetActive(true); } } diff --git a/Assets/Scripts/HealthDrawerScript.cs b/Assets/Scripts/HealthDrawerScript.cs index e978c38..4db9314 100644 --- a/Assets/Scripts/HealthDrawerScript.cs +++ b/Assets/Scripts/HealthDrawerScript.cs @@ -11,14 +11,17 @@ public class HealthDrawerScript : MonoBehaviour public void Draw(int lives) { + //there is a index out of range exception ,so check it here for(int i = 0; i < lives; i++) { - Hearts[i].sprite = FullHeart; + if(Hearts != null && Hearts.Length > i) + Hearts[i].sprite = FullHeart; } for(int i = lives; i < Hearts.Length; i++) { - Hearts[i].sprite = EmptyHeart; + if(Hearts != null && Hearts.Length > i) + Hearts[i].sprite = EmptyHeart; } } diff --git a/Assets/Scripts/PathFollower.cs b/Assets/Scripts/PathFollower.cs index 47bb7b6..a20be98 100644 --- a/Assets/Scripts/PathFollower.cs +++ b/Assets/Scripts/PathFollower.cs @@ -23,8 +23,18 @@ void OnEnable () { switch(Type) { - case PathType.Ground: path = GameObject.Find("GroundPath").GetComponent().path; break; - case PathType.Air: path = GameObject.Find("AirPath").GetComponent().path; break; + //the GroundPath and AirPath might be not found ,and cause a null reference exception + //so ,before use it ,check the null reference situation first + case PathType.Ground: + GameObject groundPath = GameObject.Find("GroundPath"); + if(groundPath != null) + path = groundPath.GetComponent().path; + break; + case PathType.Air: + GameObject airPath = GameObject.Find("AirPath"); + if(airPath != null) + path = airPath.GetComponent().path; + break; } segmentIndex = 0; @@ -58,17 +68,21 @@ void FixedUpdate () private void RecomputeSegment() { - var segment = path.GetPointsInSegment(segmentIndex); + //the path might be null reference ,so ensure it is not,and go to calculations + if (path != null) + { + var segment = path.GetPointsInSegment(segmentIndex); - A = segment[0] + PositionOffset; - B = segment[1] + PositionOffset; - C = segment[2] + PositionOffset; - D = segment[3] + PositionOffset; + A = segment[0] + PositionOffset; + B = segment[1] + PositionOffset; + C = segment[2] + PositionOffset; + D = segment[3] + PositionOffset; - v1 = -3 * A + 9 * B - 9 * C + 3 * D; - v2 = 6 * A - 12 * B + 6 * C; - v3 = -3 * A + 3 * B; + v1 = -3 * A + 9 * B - 9 * C + 3 * D; + v2 = 6 * A - 12 * B + 6 * C; + v3 = -3 * A + 3 * B; - t = 0; + t = 0; + } } } diff --git a/Assets/Scripts/Pool.cs b/Assets/Scripts/Pool.cs index d0ff4ff..80dde52 100644 --- a/Assets/Scripts/Pool.cs +++ b/Assets/Scripts/Pool.cs @@ -59,8 +59,11 @@ public void RegisterObject(GameObject prototype) public GameObject ActivateObject(string tag) { + //when run the game ,there are so many exceptions,so ,I remove the KeyNotFoundException + //and return null instead to avoid exception + //and i will handle the null situation at the caller outside this method if (!pooledObjects.ContainsKey(tag)) - throw new KeyNotFoundException(); + return null; var singlePool = pool[tag]; diff --git a/Assets/Scripts/RocketScript.cs b/Assets/Scripts/RocketScript.cs index 54443cf..4793efa 100644 --- a/Assets/Scripts/RocketScript.cs +++ b/Assets/Scripts/RocketScript.cs @@ -18,8 +18,10 @@ public class RocketScript : FlyingShotScript // Use this for initialization void OnEnable () { - Pool.Instance.ActivateObject("missileSoundEffect").SetActive(true); - + //ensure missileSoundEffect is not null reference + GameObject missileSoundEffect = Pool.Instance.ActivateObject("missileSoundEffect"); + if(missileSoundEffect != null) + missileSoundEffect.SetActive(true); shadow = transform.Find("Shadow"); shadow.position = transform.position; acceleration = InitialAcceleration; @@ -42,8 +44,9 @@ void FixedUpdate () { Target = EnemyManagerScript.Instance.GetClosestEnemyInRange(transform.position, float.PositiveInfinity, EnemyTags); if (Target == null) BlowUp(); + //if the Target is null ,the direction which calculated by target will also be null,so return here + return; } - var direction = Target.transform.position - transform.position; var angle = MathHelpers.Angle(direction, transform.up) * Time.deltaTime * Inertia * Mathf.Pow(velocity.sqrMagnitude, 0.5f); diff --git a/Assets/Scripts/StartButtonScript.cs b/Assets/Scripts/StartButtonScript.cs index d58798d..ddaca7d 100644 --- a/Assets/Scripts/StartButtonScript.cs +++ b/Assets/Scripts/StartButtonScript.cs @@ -10,12 +10,16 @@ public class StartButtonScript : MonoBehaviour private void Start() { - LoadingScreen.SetActive(false); + //the lodingScreen is unsigned ,add null reference check here + if(LoadingScreen != null) + LoadingScreen.SetActive(false); } public void StartGame() { - LoadingScreen.SetActive(true); + //the lodingScreen is unsigned ,add null reference check here + if(LoadingScreen != null) + LoadingScreen.SetActive(true); GameManager.Lives = GameManager.MaxLives; SceneManager.LoadScene("Level_01"); }