Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by archalexdima · Feb 28, 2020 at 10:09 AM · buttonsuser interfaceevents

Instanstiating activated and deactivated buttons on every update

Blockquote

Hello! I am making a super simple game and I want to use prefab buttons.(I also tried using GUI but didnt seem to help). So, every time the player clicks on a grid, they have a set of options that I want to show. I need 6 buttons, and some of them need to show as deactivated. Every button will call a method named CallForFilling() which fills the grid with an object. I have tried this

 void EnableUI()
 {
         GameObject newCanvas = Instantiate(canvas) as GameObject;
         GameObject newButton = Instantiate(button) as GameObject;
         newButton.transform.SetParent(newCanvas.transform, false);
 }

but I cannot add the method I want in the prefab from the inspector. The EnableUI() and CallForFilling() are in the same script ,attached to a "controller" game object. Any ideas?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JPhilipp · Feb 28, 2020 at 11:26 AM 0
Share

Hi! Could you please edit your question so that the code is formatted as code? Thanks!

avatar image tormentoarmagedoom JPhilipp · Feb 28, 2020 at 12:48 PM 1
Share

done... :D

avatar image ShadyProductions · Feb 28, 2020 at 02:10 PM 0
Share

I think the methods need to be public for them to show in the inspector on click window.

avatar image tormentoarmagedoom · Feb 28, 2020 at 03:05 PM 0
Share

"but I cannot add the method I want in the prefab from the inspector."

Why cannot?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Fragsteel · Feb 28, 2020 at 09:46 PM

I see two different ways of interpreting your situation.

  1. If you mean you don't have a way to specify an event you want to call in a custom script: If you declare a UnityEvent in your script and make it public (or mark it with the SerializeField attribute) then you will see an interface in the Inspector where you can add specific methods on a specific component in the scene. You might've seen it if you ever put a Button in your scene and wired up what should happen when you click it. Then when you want to call that event, you call `UnityEvent.Invoke()'.

  2. If you are actually using the Button class already and the problem is that you're not seeing the CallForFilling() method listed in the Inspector dropdown where you choose the method you want to fire when you click the button, then there are a few possible reasons:

  • The method isn't public and doesn't have the SerializeField tag over it (just needs one or the other)

  • The method has the wrong kinds of arguments - if it has none, it should be visible. It should also be visible if it has one argument and the argument is a very basic type (like a float or bool, not a custom script [and I might be wrong on this requirement]) then it should be visible

  • The method is static

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by archalexdima · Mar 10, 2020 at 09:52 AM

Thank you for your comments...The way I did it in the end is this:

And respectively I have another method DisableUI().

    public GameObject canvas;
      public List<GameObject> buttons; //those are prefab buttons
      Button bt0;
     
      void CreateUI()
         {
             GameObject newCanvas = Instantiate(canvas) as GameObject;    
             GameObject newButton0 = Instantiate(buttons[0]) as GameObject;
             bt0 = newButton0.GetComponent<Button>();
             bt0.onClick.AddListener(() => CallForFilling(buttonNames[0]));
             newButton0.transform.SetParent(newCanvas.transform, false);
             bt0.interactable = false;
     }
     //This is called from another method
      private void EnableUI()
         {       
             if (possibilities[buttonNames[0]])
             {
                 bt0.interactable = true;
             }
        }
     

I do think it is a bit of a silly way, because I have to instantiate all the buttons separately (for some reason an array of buttons that I tried to create wasnt giving the right result..), but it worked for me...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Take string from OnClick event and throw into variable. 1 Answer

Is it possible to put a world space UI onto an instantiated object? 1 Answer

NGUI Buttons in ScrollView. 0 Answers

How do I get my UI Button to play my "fire animation" once? 2 Answers

How to access the persistent listener gameobjects on a button? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges