- Home /
Reference to player won't sustain in same script
I've been getting a reference error for the player gameobject that is already set earlier in the same function.
Details that may be useful: -- This is executed directly after loading a scene (the void is called right after the SceneManager.loadscene("Game");
before any other script starts to run)
This part is towards the top, and is fine and all
PlayerStatDataBase.player = GameObject.Find("Player");
DebugLogging.Log("Found Player as GameObject '" + PlayerStatDataBase.player.ToString() + "'");
This is the part that's having troubles
PlayerStatDataBase.player.GetComponent<PlayerControl>().LoadHealth(HealthToSet);
DebugLogging.Log("Started Game Instance with player at " + playerlocation.ToString() + " and Health value of: '" + PlayerStatDataBase.player.GetComponent<PlayerControl>().Health + "'");
The very last line isn't coming up on my debug text, meaning that the line above isn't executing. It says in the unity error debug console
NullReferenceException: Object reference not set to an instance of an object
GameManager.SetPlayerStats () (at Assets/GameManager.cs:77)
LoadScene.Scene (System.String SceneName) (at Assets/DataBases/LoadScene.cs:22)
MainMenuPlaySelector.StartGame (Boolean NewGame) (at Assets/Scenes/MainMenuPlaySelector.cs:23)
UnityEngine.Events.InvokableCall`1[System.Boolean].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:188)
UnityEngine.Events.CachedInvokableCall`1[System.Boolean].Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:306)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()
Answer by Creeper_Math · Jan 02, 2017 at 05:42 PM
Realized what was going on -> the scene wouldn't be loaded when this script executed, so the objects would be null... Changed to "onlevelwasloaded" and worked fine
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
NullReferenceException 1 Answer
An object reference that seems pretty set-to-an-instance-of-an-object to me 0 Answers