- Home /
The question is answered, right answer was accepted
Calling a function via button from instance of prefab
Hello! I will try to put this as simple as possible.
I am creating an instance of a prefab which has buttons on it. If i use the "onClick()" function in the inspector the instnace wont have the script saved. (button unassigned).
My best bet was to acces the isntance, get the child as an Object and then using GetComponent getting the Button. Then i am using the method that the unity scripting api suggests (https://docs.unity3d.com/ScriptReference/UI.Button-onClick.html). My problem is that: somehow the function that is supposed to be called by the button is not. .
Here my code:openAdvBtnGo = dayDisplayInstance.transform.GetChild(5).gameObject; //Go = GameObject ==> openAdvBtnGo = GameObject openAdvBtn = openAdvBtnGo.GetComponent.(); var openAdvBtnScript : Button = openAdvBtn.GetComponent.(); openAdvBtnScript.onClick.AddListener(OpenAdvPnl);
I dont get any errors, it just doesnt execute the code in the "OpenAdvPnl" function.
I even tried putting a simple print("is this function even executed?")
in the beginning of the function but it wont work. I am seriously stuck here. Any help would be much appreciated!
Answer by DonkeyMalonkey · Feb 12, 2018 at 09:38 AM
Used a second script attacked that to the prefab and then i just assigned variables in the inspector. This worked! (was a tip from a friend)
Follow this Question
Related Questions
How to instantiate multiple Prefabs to multiple Childs randomly ? 2 Answers
Parent rigidbody using its childrens colliders. Should it work or am I stupid? 2 Answers
Instatiate to the next scene prefabs from an array 1 Answer
Prefab clones aren't deleting? 1 Answer
Best way to emulate Swift String Interpolation in UnityScript? 1 Answer