The first issue I have is that even though my UIs work fine, whenever I make an image enabled when it is currently not enabled, I get the error message Null Reference Exception. Why does it give that error when the image appears and disappears on command? There is nothing wrong. Secondly, I have a function that pauses the game and another to resume play. I have the key "P’ to pause and resume play. It works perfectly. However, when I press ‘H’ for the help screen, it pauses the game as it is supposed to and displays the help screen perfectly. When I press ‘H’ again, it is supposed to un-enable the help screen, which it does, and then resume the game. But instead of resuming, it locks up the game. The pause and resume are in one script and the handling of the buttons is in another. I am using Unity 2020.2.7f1. The public variables listed are present in the Inspector for the appropriate script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public void PauseGame()
{
Time.timeScale = 0;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
FindObjectOfType<WeaponSwitcher>().enabled = false;
}
public void ResumeGame()
{
Time.timeScale = 1;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
FindObjectOfType<WeaponSwitcher>().enabled = true;
Debug.Log("Game should be active again");
}
}
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PanelControl : MonoBehaviour
{
public Image keyImage;
public Button helpButton;
public Button pauseButton;
public Button escButton;
public Image helpScreen;
bool helpScreenShowing = false;
bool gamepaused = false;
GameManager gm;
private void Start()
{
gm = FindObjectOfType<GameManager>();
keyImage.enabled = false;
helpScreen.enabled = false;
}
private void Update()
{
if (Input.GetKeyUp(KeyCode.H))
{
ShowHelpScreen();
}
else if (Input.GetKeyUp(KeyCode.P))
{
PauseGameForNow();
}
else if (Input.GetKeyUp(KeyCode.Escape))
{
if (gamepaused)
{
PauseGameForNow();
}
else
{
SceneManager.LoadScene(0);
}
}
}
private void PauseGameForNow()
{
if (gamepaused)
{
gm.ResumeGame();
}
else
{
gm.PauseGame();
}
gamepaused = !gamepaused;
}
public void ShowHelpScreen()
{
if (helpScreenShowing)
{
helpScreen.enabled = false;
gm.ResumeGame();
}
else
{
gm.PauseGame();
helpScreen.enabled = true;
}
helpScreenShowing = !helpScreenShowing;
}
public void EnableKey()
{
keyImage.enabled = true;
}
}
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
I am not complaining that my code does not work as for the null reference, I am saying that everything works just fine and I am wondering why I get the error message when everything is working. Am I not making a proper instance of the help screen or other buttons/images? Also, the resume play has not been addressed yet.
Many times if everything appears to work, but you get a null error, it’s possible you accidently added a duplicate of your script to the scene and it’s throwing the null error while the first copy of your script, which is setup correctly, is working as intended.
Otherwise, the null error points you to the line that is throwing the error, so I would double check that line.
I have thoroughly checked to see if the script is a component of another game object. It is not. Visual Studio gives references to the classes and methods also so I can track down whatever references the classes and methods. I not only visually checked every game object but also looked at all of the references to the scripts. The only references is when I call a specific method from another script. But the references do not call the methods that are giving the error messages. If the null reference error messages were real errors, it would not allow the things I tell it to do because it would have no idea what I am referring to. But the methods work and I really think that these are, in reality, false errors. Please show me where I am wrong.
If the help screen is null, why does it display when I hit ‘H’ and then go away when I hit ‘H’ again? I can do it multiple times and it behaves nicely. The only thing is that it gives a “supposed” error of it being null. I think Unity is lying.
Add Debug.Log("Helpscreen: " + helpScreen, gameObject); right before line 71. When you see this log, click on it and it will flash the gameobject that is throwing the message. Check that gameobject and see if it’s what you expect it to be.
I know that it is the image of the help screen that is “throwing” the error. What I am telling you is that I have set up everything correctly. I have the help screen image as a public variable on the script, and it functions exactly as it is supposed to with the correct coding. If you see where I need to put in another line of code to make it work where it shows no errors, tell me what it is. I have searched on youtube for making images visible and they do the same thing I do. They don’t talk about any errors generated. I don’t know if they notice them because everything works just fine as programmed. I think that Unity says that the image is null is because the image starts out being disabled and then I enable it. I therefore am insisting that it is a false error because everything works fine in the game. This type of error also shows up for some enemies where their navmesh is out of range to the player where it may give a similar type of error and nothing is really wrong. The key point is that if I really did something wrong and there really is an error, as I mentioned specifically before, then the help image would never show up! Since it does show up, my code is correct and Unity is just giving some errors by mistake and it doesn’t really affect the game…
And I was telling you that the debug code will help make sure you don’t have a second gameobject in the scene with that script on it. Honestly, I can’t help you anymore than I have. If you believe Unity is lying to you, then move on and keep on coding. Nothing @Kurt-Dekker or I say is changing your mind about it. You are so convinced that Unity is in the wrong that I don’t know what help you expect. Happy Coding!
I can confirm that this stripped-down version works without a NullRef exception. I don’t know what that means in context of the thread, but just thought I’d try it…
using UnityEngine;
using UnityEngine.UI;
public class testNullRefForum : MonoBehaviour
{
public Image helpScreen;
bool helpScreenShowing = false;
private void Start()
{
helpScreen.enabled = false;
}
private void Update()
{
if (Input.GetKeyUp(KeyCode.H))
{
ShowHelpScreen();
}
}
public void ShowHelpScreen()
{
if (helpScreenShowing)
{
helpScreen.enabled = false;
}
else
{
helpScreen.enabled = true;
}
helpScreenShowing = !helpScreenShowing;
}
}