Declared variable being returned as null in a method?
I'm stumped as to what the solution to this problem is, and I've been looking for an answer for days to no avail. The problem is that when one particular method tries to access a public gameObject that's been assigned in the editor, it returns as null. If I check anywhere else in the script, it returns the object just fine. It's just the one method that returns it as null. Here's the code:
using UnityEngine;
using System.Collections;
public class HackableController : MonoBehaviour {
public bool readyToHack;
public bool puzzleActive;
public GameObject hackPuzzle;
public GameObject hackObj;
private GameObject playerOne;
private Player playerController;
private PuzzleController puzzleController;
void Start()
{
playerOne = GameObject.FindGameObjectWithTag("PlayerOne");
playerController = playerOne.GetComponent<Player>();
puzzleController = hackPuzzle.GetComponent<PuzzleController>();
}
void Update()
{
if (readyToHack && !puzzleActive)
{
if (Input.GetKeyDown(KeyCode.E))
{
puzzleActive = true;
playerController.canMove = false;
hackPuzzle.gameObject.SetActive(true);
puzzleController.StartObstacleSpawn();
}
}
else if (readyToHack && puzzleActive)
{
if (Input.GetKeyDown(KeyCode.E))
{
puzzleActive = false;
playerController.canMove = true;
puzzleController.DestroyPuzzleObstacles();
hackPuzzle.gameObject.SetActive(false);
}
}
}
public void OpenHackableObject()
{
Debug.Log("Door Object: " + hackObj);
hackObj.gameObject.SetActive(false);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "PlayerOne")
{
readyToHack = true;
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "PlayerOne")
{
readyToHack = false;
}
}
}
The problem occurs in the "OpenHackableObject" method. The method is called from another script, if that makes a difference, but passes in no variables of its own. There is also no NullReferenceException, it simply doesn't work.
Can you show how you call it in your other class, because the only thing I can think of is that you are calling an new 'empty' instance of your HackableController class.
So make sure in your other script you are referring to this exact class.
The method is called just fine, it just can't access the hackObj variable for some reason.
But that can't be,
If you get a NULL output because it's been called from another script, means the script has an empty reference of HackableController script.
But if you call OpenHackableObject inside the OnTriggerEnter2D it output is still null?
so like
public void OpenHackableObject()
{
Debug.Log("Door Object: " + hackObj); //output null?
hackObj.gameObject.SetActive(false);
}
void OnTriggerEnter2D(Collider2D other)
{
Debug.log("[ TEST ] "+hackObj); //output hackobj?
OpenHackableObject();
if (other.tag == "PlayerOne")
{
readyToHack = true;
}
}
The nearest idea that I have about this is that the PuzzleController does not reference a GameObject in the scene (hierarchy) but a prefab from the project view that is not instantiated. This would at least explain why nothing happens but there's also no NullReference, because the wired prefab might have correctly set references on itself but is not part of the current game.
You named those variable ...prefab which made me curious.
Other than that, set breakpoints in $$anonymous$$onoDevelop and see where you're getting.
It gets to the OpenHackableObject method in the HackableController, which is where it runs into the issue of the door being null, despite being set in the editor.
Is this a 2D game? Could you show a screenshot of the inspector when the game is running?
Your answer
Follow this Question
Related Questions
re historic GUI system 1 Answer
Method Skipping Inputs 2 Answers
How to access a variable from another C# script in Unity 1 Answer
I can't seem to have access to Tilemap methods, what am I doing wrong ? 0 Answers
List.FindIndex 1 Answer