Unity - Call a Function from Script Using Button On Click Event

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this video will show how to call a function from script using a button on a click event so first i'll create a ui element it's a button text mesh pro and ask me if i want to import tmp essentials or say yes and okay that's done and we'll name the button maybe on click button okay i'll change it to 2d so we can see okay there we go that's the canvas uh will change the text of the button to on click button and change the the size font size to 18 and maybe change the button background to green yeah why not and then we'll change the positions the anchor to be top left and the pivot to be zero on the x and one on the y and change the position to 15 for the x and minus 15 for the y okay so that's ready and then we'll create and just for testing we'll create a new empty object under the canvas and we'll call it on click pop-up inside this empty object it would be a parent of two new objects one is a panel so we'll go create ui look for panel there we go and we'll call the panel on a click panel and we'll um go to the panel change the background to none and change the color to similarly green and um uh keep the anchor to the center keep uh the pivot to the middle or point f oh sorry 0.5 from both the x and y and maybe change the width to 800 or maybe 600 and the height to 400 or maybe even less 300 okay and i'll change the transparency to none okay and now create another a new ui object inside the group and it set text ui text mesh brow there we go and we'll call it on click text and inside the text i'll just say on click button is in for invoked okay and we'll change the size to 25 maybe and the vertex color to black and um yeah that seems or maybe even the size less i will keep it 20 and increase the width to 400 yeah that sounds fine and keep the alignment to the center okay so that's just for testing now well that's done i'll create a new script here and we'll name it on click and i will assign the script to the canvas object just make sure it's there yeah there it is and so now i'll open the script here it is and okay so and we'll create a public game object we'll call it on click pop pop i'll save that go back to the editor and go to the script which is under the canvas here we go and here i could assign the new public game object to which i will drag the on click pop-up parents too that's done and while we are here and i'll just make that uh inactive or invisible okay so save that and we'll go back to the uh script and i will add another method make sure it's public so we can as access it from the editor and of type void and maybe we'll call it click and um we'll call that object on click popup only click popup dot set active and we'll set that to sorry we'll send that to true that's it i'll save that and go back to the editor and now as i go to the button on the click button and go to add an event to which i will drag the canvas because this create script on clickscript is attached to it so i'll drag that canvas here and now i could access the public methods inside that object and this case it's on a click script which is this one i'll access the script attached to it and then all the public methods inside that script and this case it's click which is uh here okay okay so now once we click on the button this function would be called and that function or method will check change the active status of only click popup group to true in other word change the this status which is inactive now here to active okay to test that i'll hit play and click the button and there you go the the button has called the only click event on the button has called that method and that script and that's it this is how you can call a function form script using button on click event thank you
Info
Channel: DA LAB
Views: 20,773
Rating: undefined out of 5
Keywords: Unity, On Click Event, Button
Id: I63uHchmCxc
Channel Id: undefined
Length: 9min 4sec (544 seconds)
Published: Sun Jul 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.