Search Unity

EventSystem.current.currentSelectedGameObject.name NullReferenceException

Discussion in '2D' started by RiccaD, Jan 14, 2020.

  1. RiccaD

    RiccaD

    Joined:
    Jul 20, 2017
    Posts:
    1
    In my MenueScene I have a few Buttons in a Scroll Rect in a Canvas and when i transition from one Menue to the other one I deactivate (Interactible == false) all Buttons in the Scene until the transition is finished. Then I activate the Buttons on Screen with the .setOnComplete from the LeanTween PlugIn.
    Sometimes, not always, if i click a Button exactly after the Animation is finished this error is showing up:
    NullReferenceException: Object reference not set to an instance of an object
    and it points to the onClick method where im trying to get the name of the Button that is pressed with EventSystem.current.currentSelectedGameObject.name.

    //Animation

    Code (CSharp):
    1.  
    2. public static void slideAnimation(int direction, GameObject alt, GameObject neu, bool destroy) {
    3.         foreach (GameObject o in GameObject.FindGameObjectsWithTag("zoom")) {
    4.             if (o.GetComponent<Button>() != null) o.GetComponent<Button>().interactable = false;
    5.         }
    6.         if (neu != null) {
    7.             neu.name = neu.name.Replace("(Clone)", "");
    8.             neu.transform.SetParent(GameObject.FindGameObjectWithTag("canvas").transform);
    9.             neu.transform.localScale = new Vector3(1, 1, 1);
    10.             neu.transform.localPosition = new Vector3(-direction * GameObject.FindGameObjectWithTag("canvas").GetComponent<RectTransform>().rect.width, 0, 0);
    11.             LeanTween.moveLocalX(neu, 0, 0.5f).setEase(startAnimation.bouncycurve).setOnComplete(() => {
    12.                 foreach (GameObject o in GameObject.FindGameObjectsWithTag("zoom")) {
    13.                     if (o.GetComponent<Button>() != null) o.GetComponent<Button>().interactable = true;
    14.                 }
    15.             });
    16.         }
    17.         if(alt != null) LeanTween.moveLocalX(alt, direction * GameObject.FindGameObjectWithTag("canvas").GetComponent<RectTransform>().rect.width, 0.5f).setEase(startAnimation.bouncycurve).setDestroyOnComplete(destroy);
    18.     }
    19.  
    //onClick

    Code (CSharp):
    1.  
    2. public void onUpperCategory() {
    3.         string helper = EventSystem.current.currentSelectedGameObject.name;
    4.         GameObject alt = GameObject.FindGameObjectWithTag("slide");
    5.         GameObject neu = Instantiate(Resources.Load("Prefabs/Categories/Ca" + helper, typeof(GameObject)), Vector3.zero, Quaternion.identity) as GameObject;
    6.         sV.slideAnimation(-1, alt, neu, true);
    7.     }
    8.  
    Yes i have ONE EventSystem in my Scene at all times

    If someone could help my I would be very thankfull, Cheers Ricca
     
  2. LLZ_T

    LLZ_T

    Joined:
    Apr 22, 2020
    Posts:
    8
    Necroed. "EventSystem.current.currentSelectedGameObject" is generally null when UI elements are not in focus.