- Home /
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?
Hi! Could you please edit your question so that the code is formatted as code? Thanks!
I think the methods need to be public for them to show in the inspector on click window.
"but I cannot add the method I want in the prefab from the inspector."
Why cannot?
Answer by Fragsteel · Feb 28, 2020 at 09:46 PM
I see two different ways of interpreting your situation.
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()'.
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
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...
Your answer
Follow this Question
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