Search Unity

Disabling canvases in an array on another script

Discussion in 'Scripting' started by Roryyyyyyyyyy, Aug 5, 2016.

  1. Roryyyyyyyyyy

    Roryyyyyyyyyy

    Joined:
    Jun 8, 2015
    Posts:
    22
    Hi, so I have a script for general UI and a more specific script for one of my menus. The general ui script holds the different menus in an array and iterates through the array to disable them all/enable the active one. I have a function that toggles them but when I try to call that function from another script it says that the object reference is not set to an instance of an object.

    I think I know why that is happening, please correct me if I'm wrong but I'm guessing the instances in the array are only accessible from that script?

    How would I change this so that the function would work outside of the script.

    UI Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class UIScripts : MonoBehaviour
    5. {
    6.  
    7.     public bool menuOpen = false;
    8.  
    9.     public GameObject[] uiList;
    10.     int currentMenu = 0;
    11.  
    12.  
    13.     // Use this for initialization
    14.     void Start ()
    15.     {
    16.        
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         OpenCloseMenu();
    23.  
    24.         if (Input.GetKeyDown("escape"))
    25.         {
    26.             if(menuOpen == true)
    27.             {
    28.                 menuOpen = false;
    29.             }
    30.             else
    31.             {
    32.                 menuOpen = true;
    33.             }
    34.         }
    35.     }
    36.  
    37.     //Checks if menu is open, if yes, opens current selected menu
    38.     public void OpenCloseMenu ()
    39.     {
    40.         if (menuOpen == false)
    41.         {
    42.             for (int i = 0; i < uiList.Length; i++)
    43.             {
    44.                 uiList[i].SetActive(false);
    45.             }
    46.         }
    47.         else
    48.         {
    49.             for (int i = 0; i < uiList.Length; i++)
    50.             {
    51.                 if (i != currentMenu)
    52.                 {
    53.                     uiList[i].SetActive(false);
    54.                 }
    55.                 else
    56.                 {
    57.                     uiList[i].SetActive(true);
    58.                 }
    59.  
    60.             }
    61.         }
    62.     }
    63.  
    64.     public void CloseMenus()
    65.     {
    66.         for (int i = 0; i < uiList.Length; i++)
    67.         {
    68.             menuOpen = false;
    69.             uiList[i].SetActive(false);
    70.         }
    71.     }
    72. }
    73.  
    BuildingUI Script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BuildingUI : MonoBehaviour {
    5.  
    6.     [SerializeField]
    7.     GameObject player;
    8.  
    9.     [SerializeField]
    10.     GameObject uiManager;
    11.  
    12.     ObjectPlacement objectPlacementScript;
    13.  
    14.     UIScripts uiScript;
    15.  
    16.     // Use this for initialization
    17.     void Start ()
    18.     {
    19.         objectPlacementScript = player.GetComponent<ObjectPlacement>();
    20.         uiScript = transform.parent.GetComponent<UIScripts>();
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update ()
    25.     {
    26.  
    27.    
    28.     }
    29.  
    30.     public void SetBuilding(int button)
    31.     {
    32.         objectPlacementScript.SetObject(button);
    33.         uiScript.CloseMenus();
    34.     }
    35. }
    36.  

    The SetBuilding function is called when the button is pressed. It is only the uiScript.CloseMenus which is causing the problem.

    Cheers in advance
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    nope, public array, public function; you can access both the array from anywhere in the scene, and call the function from anywhere in the scene.


    are you referring to line 33 in the second script? if that is throwing a null ref exception then there is an issue with setting up the reference on line 20.
     
  3. Roryyyyyyyyyy

    Roryyyyyyyyyy

    Joined:
    Jun 8, 2015
    Posts:
    22
    Daft one I am, thanks for pointing that out! It was literally the transform.parent. part messing it up!
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Also you may want to put your openclosemenu() call within your if statement so it's not constantly running that method and doing the same thing over and over. You really only need them turned on or off when the button is pressed.