NullReferenceException when attempting to call a GameObject from within PointerEventData
I'm using a script to grab the name of the button and depending on that name it opens a certain menu. So if Play is clicked, a save select or player stats menu appears (haven't started on that yet) and if Options is clicked it opens the settings, or the Close button will close a menu.
Right now I'm starting on the settings part, but whenever I refer to the settings screen's GameObject it gives me a nullreference exception, starting from line 31.
... if( SettingsListA.activeSelf == false && MyName == "OptionsButton"){ ...
Here is the current setup I have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
public class OpenSaveSelect : MonoBehaviour , IPointerClickHandler {
public Animator MenuboxAnim;
public GameObject MenuboxMaster;
public GameObject SettingsListA;
public string MyName;
// Use this for initialization
void Start () {
MenuboxAnim = gameObject.GetComponentInParent<Animator>();
MenuboxMaster = GameObject.FindWithTag("menupanelA");
SettingsListA = GameObject.Find("SettingsA");
MyName = gameObject.ToString();
}
// Update is called once per frame
void Update () {
}
public void OnPointerClick(PointerEventData pointerEventData){
//if(pointerEventData.button == PointerEventData.InputButton.Left){
if( SettingsListA.activeSelf == false && MyName == "OptionsButton"){
SettingsListA.SetActive(true);
MenuboxMaster.SetActive(false);
} else if ( SettingsListA.activeSelf == true && MyName == "CloseSettingsDialog") {
SettingsListA.SetActive(false);
MenuboxMaster.SetActive(true);
}
//}
}
}
The script isn't 100% complete yet because I'm still trying to fix all of this.
And that happens whenever I click any of them. Apparently it can't recognize the var SettingsListA as a GameObject?
Ok, take this with a grain of saLt, as I'm still trying to figure out the Event system's quirks, blind leading the blind and all that, but...
Perhaps try using the raycast target directly via pointerEventData.pointerCurrentRaycast.gameObject
For my project I created a reference public GameObject currentHover;
public void OnPointerEnter(PointerEventData ped) { currentHover = ped.pointerCurrentRaycast.gameObject; } public void OnPointerExit(PointerEventData ped) { currentHover = null; }
Then when I wanted to have DoSomething(); fire only over a particular GO I went with the inefficient ham-fisted method.
if (currentHover && (currentHover.name == this.gameObject.name) { DoSomething(); }
Hope that helps at least give you more levers to tug on.
Cheers
Answer by JVene · Aug 30, 2018 at 07:09 AM
Such an exception on line 31 would be one of 3 possibilities.
Double check with debugging in Start to make sure SettingsListA is actually found. It will be null if not, and that will cause the exception.
Likewise, double check that MyName is in fact formed. I can't see how that fails, but check everything when diagnosing to be sure, I don't know all possible return values from "ToString" could be in that context. Maybe it can return null.
Is there any chance the OnPointerClick method is called before Start executes? I seriously doubt it in practice, but in theory it might - but only for a fraction of a second (Start is called by the framework under synchronization with various cycles of events, but OnPointerClick fires from messages, quite possibly in it's own thread). This is a very longshot possibility, and I doubt it could apply.
Debug to verify (discover) which is actually null. Is it SettingListA or MyName?