- Home /
Script forgets assigned Inspector variables
Hi all, I have recently Updated my Character Controller to a more advanced one to finally rid my game of the horrid sluggish Unity provided one, I am now going through all the scripts to change the 'stopping' methods while paused/interacting ect. When I pause the game all works as expected the Walking Controller is disabled, so the player cannot move, also the Camera Controller also gets disabled as expected this stops the camera moving around while the mouse is moving while paused. Though the moment I try to click resume, it has forgotten what the 'aWC' variable was, so it attempts to get the component from the Player (the object this script is attached to) both the 'Player' and 'aWC' of which are Set as '[SerializeField]'s as can be seen in the screenshot below, the Inspecter still seems to be holding the variables, but is unable to reference them what so ever.
My code for handling the the Resume Function is: (the reasoning for all the null checks is becuase it kept failing)
public void onResume()
{
if(aWC != null)
{
aWC.enabled = true;
}
else
{
aWC = plr.GetComponent<AdvancedWalkerController>(); //ERROR - 'plr' not assigned.
aWC.enabled = true;
}
if(camCon != null)
{
camCon.enabled = true;
}
else
{
camCon = chr.GetComponent<CameraController>();
camCon.enabled = true;
}
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
pausedCanvas.SetActive(false);
isPaused = false;
}
I have also attempted to try the code as such:
if(aWC != null)
{
aWC.enabled = true;
}
else
{
if(plr != null)
{
aWC = plr.GetComponent<AdvancedWalkerController>();
aWC.enabled = true;
}
else
{
plr = gameObject;
aWC = plr.GetComponent<AdvancedWalkerController>();
aWC.enabled = true; //ERROR PRINTOUT RETURNS HERE 'NullReferenceException'
}
}
with the problem then trurning into a viscious cycle and then forgetting the 'aWC' script Right after it has just re-assigned it. Could it be because I am disabling/enabling the 'aWC' and 'CamCon' Scripts? As this was the way I did it with the Unity Character, it never forgot who the FPC was nor the Player.
Any leads in the right direction would be greatly appreciated! Thank you and Kind Regards!
first of all, check if this is the only "pause game" component in the scene.
is a pretty common error, duplicate component and later u cannot identify what is the one with the error.
Hi @DCordoba, yes this is certainly the only object with the pauseScript attached to is the new Player, the old player has already been removed from the game, which use to have a pause script, that used similar functions (disabling the movement script while paused) to stop the player from moving.
as a debugging suggestion, add an update method in the Pause script with
if(plr == null) Debug.Log("MY NAME IS" + gameObject.name + "AND PLR NOT ASSIGNED");
so that you can make sure there are no duplicates and when it loses the reference
Answer by FuryFight3r · Apr 09, 2021 at 10:57 PM
The problem was on the button itself, the onClick() event was still trying to reference the old Player Pause Script not the new one, simply a rookie mistake :P
Answer by Nistroy · Apr 07, 2021 at 10:47 AM
Why not do that? Remove the plr variable and just put GetComponent<>() @FuryFight3r
public void onResume()
{
if(aWC != null)
{
aWC.enabled = true;
}
else
{
aWC = GetComponent<AdvancedWalkerController>();
aWC.enabled = true;
}
if(camCon != null)
{
camCon.enabled = true;
}
else
{
camCon = chr.GetComponent<CameraController>();
camCon.enabled = true;
}
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
pausedCanvas.SetActive(false);
isPaused = false;
}
Hi thanks for the suggestion though in every single script for my project even if it is attched to the 'Player' itself I will always reference the player as 'plr' so it is consistent between all scripts