Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

Can someone make sense out of seemingly inconsistent null reference exception?

$
0
0
Today I encountered the weirdest error, I hope someone can make sense out of this cause I'm baffled. I am a relative noob to Unity so please forgive and point out any wrong practices or design patterns. I have a class (below) that lets me add ammo to actors. It sends the ammo types to the component responsible for instantiating them, BeamFire and ProjectileFire; who both inherit from Fire. When I run this code, I get an error at the line Debug.Log("AddAmmoInManager" + GetFireComponent(ammoType).ToString()); because both beamFire and projectileFire are null. However, when I checked those field after initialising, in Start(), they display the correct component. What is going on here? Thanks so much for your time, this really has been frustrating me all day. **EDIT** A little bit more info: More to the point: why is beamFire and projectileFire null at line 37 when it's not at line 14? They don't seem to be destroyed externally, I say this because when the editor pauses I can still see both components on the gameobject. I call the AddAmmo from another script after Start(), and my console output is: LeftPlayer (ProjectileFire) UnityEngine.Debug:Log(Object) ActorAmmoManager:Start() (at Assets/Scripts/Ammo/ActorAmmoManager.cs:17) LeftPlayer (BeamFire) UnityEngine.Debug:Log(Object) ActorAmmoManager:Start() (at Assets/Scripts/Ammo/ActorAmmoManager.cs:18) Projectile1 UnityEngine.Debug:Log(Object) ActorAmmoManager:AddAmmo(AmmoType) (at Assets/Scripts/Ammo/ActorAmmoManager.cs:23) c__Iterator0:MoveNext() (at Assets/Scripts/WorldCreation/CreatePlayer.cs:60) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) Null UnityEngine.Debug:Log(Object) ActorAmmoManager:GetFireComponent(AmmoType) (at Assets/Scripts/Ammo/ActorAmmoManager.cs:37) ActorAmmoManager:AddAmmo(AmmoType) (at Assets/Scripts/Ammo/ActorAmmoManager.cs:24) c__Iterator0:MoveNext() (at Assets/Scripts/WorldCreation/CreatePlayer.cs:60) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) Null UnityEngine.Debug:Log(Object) ActorAmmoManager:GetFireComponent(AmmoType) (at Assets/Scripts/Ammo/ActorAmmoManager.cs:38) ActorAmmoManager:AddAmmo(AmmoType) (at Assets/Scripts/Ammo/ActorAmmoManager.cs:24) c__Iterator0:MoveNext() (at Assets/Scripts/WorldCreation/CreatePlayer.cs:60) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) GetFireComponentProjectile1 UnityEngine.Debug:Log(Object) ActorAmmoManager:GetFireComponent(AmmoType) (at Assets/Scripts/Ammo/ActorAmmoManager.cs:39) ActorAmmoManager:AddAmmo(AmmoType) (at Assets/Scripts/Ammo/ActorAmmoManager.cs:24) c__Iterator0:MoveNext() (at Assets/Scripts/WorldCreation/CreatePlayer.cs:60) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) NullReferenceException: Object reference not set to an instance of an object ActorAmmoManager.AddAmmo (AmmoType ammoType) (at Assets/Scripts/Ammo/ActorAmmoManager.cs:24) CreatePlayer+c__Iterator0.MoveNext () (at Assets/Scripts/WorldCreation/CreatePlayer.cs:60) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent (typeof (BeamFire), typeof(ProjectileFire))] public class ActorAmmoManager : MonoBehaviour { private ProjectileFire projectileFire; private BeamFire beamFire; void Start() { projectileFire = gameObject.GetComponent (); beamFire = gameObject.GetComponent (); Debug.Log (projectileFire); Debug.Log (beamFire); } public void AddAmmo(AmmoType ammoType) { Debug.Log (ammoType); Debug.Log("AddAmmoInManager" + GetFireComponent(ammoType).ToString()); GetFireComponent(ammoType).AddAmmo (ammoType); } public void RemoveAmmo(AmmoType ammoType) { Debug.Log ("RemoveAmmo"); GetFireComponent (ammoType).RemoveAmmo (ammoType); } private Fire GetFireComponent(AmmoType ammoType) { Debug.Log (projectileFire); Debug.Log (beamFire); Debug.Log ("GetFireComponent" + ammoType); if (AmmoData.IsProjectile (ammoType)) return projectileFire; if (AmmoData.IsBeam (ammoType)) return beamFire; Debug.Log ("Error: ammoType is unknown"); Debug.Break (); return null; } }

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>