Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

assigning gameobject in a static function

Discussion in 'Scripting' started by bobcccc, Mar 22, 2020.

  1. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    I am writing a static function to be called in any of my scripts that will show a modal box with a message in it. I can successfully call the function from all of my scripts but it is not assigning the gameobject of the modal box


    Code (CSharp):
    1.  
    2. public static GameObject MsgBox;
    3. public static void domessage(string message)
    4.     {
    5.          MsgBox = GameObject.Find("MsgBox");
    6.          MsgBox.SetActive(true);
    7.         Debug.Log("msgbox");
    8.     }
    The Panel of the message box is called MsgBox, any help on being able to assign the gameobject to the script would be great thanks.
     
  2. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    actually when the box is not hidden to begin with it works fine...I am guessing the Find does not see hidden gameobjects?
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Find doesn't see hidden gameobjects. you're supposed to cache your references when you initialize them.
    otherwise, why do you need that
    static GameObject MsgBox;
    variable? you coud've achieved the same with a variable local to the function.

    The idea is to initialize your class when the time is proper (your game start, most likely).
    Code (csharp):
    1. static public UtilityUI {
    2.  
    3.   static GameObject _msgbox;
    4.  
    5.   static public InitMessageBox(GameObject msgbox) {
    6.     _msgbox = msgbox;
    7.   }
    8.  
    9.   static public ShowMessageBox(string message) {
    10.     _msgbox.GetComponent<TextBox>().text = message; // or whatever
    11.     _msgbox.SetActive(true);
    12.   }
    13.  
    14.   static public HideMessageBox() {
    15.     _msgbox.SetActive(false);
    16.   }
    17.  
    18. }
    Then you use this elsewhere, for example you make a GameManager MonoBehaviour that is accessible from everywhere
    Code (csharp):
    1. public class GameManager : MonoBehaviour {
    2.  
    3.   public static GameManager Instance { get; private set; }
    4.  
    5.   void Awake() {
    6.     Instance = this;
    7.     UtilityUI.InitMessageBox(GameObject.Find("msgbox"));
    8.   }
    9.  
    10.   public ShowMessageBox(string message) => UtilityUI.ShowMessageBox(message);
    11.   public HideMessageBox() => UtilityUI.HideMessageBox();
    12.  
    13. }
    This is all just an example.
     
  4. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    ah I see I am new to c# but not programming, this will come in very helpful thankyou.
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    It's ok, but don't treat this example as set in stone, because there are literally thousands of ways to achieve this or something similar, not all of which are the best, and some may even be the worst, including this example of mine.

    In it, UtilityUI is completely redundant, and the whole thing is of dubious value, so take all of that with a grain of salt.
    It was intended as an illustration.

    Your original concept of having a separate utility class with static methods to handle this use-case scenario, is a fertile one, and I admire your willingness to separate such logic from the rest of the system, that's good thinking on your part, but it needs refining to be truly useful.

    And Find is weird btw, and can be potentially very slow. It is imperative to use it only if you truly must, and do it once, cache your references, and never do Find again.

    In a typical usage scenario, however, you can ignore Find completely, and link drag-n-drop the prefab directly to your serialized GameObject field on a script. Then use this reference from the script.
     
  6. biruktes1

    biruktes1

    Joined:
    Nov 15, 2020
    Posts:
    2