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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I get multiple UI elements to share the same instance of a script

Discussion in 'UGUI & TextMesh Pro' started by Phanixis, Dec 4, 2015.

  1. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    Here is my problem, I have created several UI buttons and a single script called MenuManager to handle this input from each of these buttons. I have added this script as a component to each UI button, and I have set different public methods of the script to the "On Click ()" function.

    Based on a debug.log I have placed with MenuManager.start, Unity appears to be generating a separate instance of MenuManager for each button I have attached this script component to. This is unnecessary as I only require a single instance of this script and will cause me all sorts of problems down the road. How do I get all my UI elements to share a single instance of this script?

    Thanks for the help.
     
  2. jprocha101

    jprocha101

    Joined:
    Apr 8, 2015
    Posts:
    134
    Hi @Phanixis - Don't place menumanager on each button. Each instance of the script as a component will create just that.

    Instead you can add the menumanager to one object in your hierarchy. I would use the canvas or create a "manager" game object to hold the menumanager script instance.

    After you add the script to the manager/canvas object you can drag that object into each button component then select the on click methods.

    Hope this helps.
     
  3. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    Ok, I still can't get this to work.

    I went ahead and created an new empty game object in my hierarchy called MenuManager, and added the script MenuManager as a component to this empty object. This will be the only place in my hierarchy the script will be referenced.

    Unfortunately I cannot seem to reference the MenuManager object in my UI element and drag and drop doesn't seem to work (it just shows the circle with the slash if I attempt this). I could save my MenuManager object as a prefab and reference that in my UI elements, but I think that would just bring back the original problem of each button creating a new instance of MenuManager, the only difference being it would be an object with the script attached instead of the script class itself.
     
  4. jprocha101

    jprocha101

    Joined:
    Apr 8, 2015
    Posts:
    134
    Where are you trying to drop it? It should let you drop it in the on click area of your button component in the inspector. There is a slot on the left where you place it.

    Click the + in the on click section and that will expose the area you drop the object.





    Not sure how experienced you are with Unity so may be overkill :)
     
    Last edited: Dec 4, 2015
  5. Phanixis

    Phanixis

    Joined:
    Oct 19, 2015
    Posts:
    27
    Ok, I figured out what I was doing wrong. I was referencing the buttons to themselves for On Click so I could access the script that I originally attached directly to the buttons, which struck me as an extraneous extra step. Now I have all the buttons referencing the MenuManager object and my menus are working correctly.

    Thanks for the help.
     
    jprocha101 likes this.
  6. jprocha101

    jprocha101

    Joined:
    Apr 8, 2015
    Posts:
    134
    No problem. Glad to help.