Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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 at 9:21 PM.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    185
    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:
    1,980
    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:
    185
    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:
    1,980
    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.