Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Set Onclick function unity

Discussion in 'Scripting' started by Vexer, Aug 12, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    186
    Hey guys!

    I had a very annoying problem with my buttons and that was that my values that the buttons would update were not updating untill i stopped my game so i found out that that is because i was actually changing the values of my prefab instead of my actual gameobject in the hierarchy the only issue that i have right now is that i instantiate my UI because when i instantiate my UI i can't drag and drop my gameobject to the onclick function in the inspector so my question is if there is a way to set the onclick function object and what it needs to do right now i have this:

    Code (csharp):
    1.  
    2. public void start(){
    3. Slot1.onClick.AddListener(CollectGold);
    4. }
    5.  
    6. public void CollectGold()
    7.     {
    8.         goldmanager.RandomGoldAmount();
    9.         goldmanager.Gold += goldmanager.lvl1;
    10.         Debug.Log("Collected Gold!!");
    11.         Destroy(GameObject.FindWithTag("LootSlot1"));
    12.     }
    13.  
    14.  
    But i need to find a way to set the onclick function in my script (this part: https://gyazo.com/85259b73386bfc3bf91818b912cec65c )
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,080
    In other words, the button you're currently referring to by 'Slot1' is part of the prefab? Or what's the Slot1 button got to do with in?

    Just subscribe to the events on the scene instance, or after instantiation.

    From a general point of view though, I wouldn't talk to any button directly. Instead, I'd write a component that wraps around your UI and provides events that you can subscribe to. This allows you to be flexible with the implementation.
     
  3. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    186
    What i mean is the (Object) in the screenshot i've shown used to be a prefab and that caused a problem with my values not updating correctly but for some reason i can't change the object to a gameobject and i need to achieve that..
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,080
    You can basically set anything there.
    I'm not entirely sure about your prefabs, though if the target is part of the prefab's hierarchy , it'll be instantiated correctly and the instance is going to use the object from its own hierarchy aswell.

    However, if these are seperate prefabs, you'll either need to have both instantiated in the scene from the beginning and link the instance, or if instantiation within code plays a role, you'll need to instantiate the prefab and set everything up programmatically.