How to use Unity's Toggle UI element with scripts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so to take advantage of a toggle button here's what you want to do you go to your canvas first thing you do is you create a UI toggle now imagine you want to toggle something about your game and your game has your game have a script let's see if a player object with a player script on it so let's open up the player script and we're gonna create a public void user toggle and we're gonna pass the pool and we call it top again so that toggle or that tog I'm just gonna print out the value print okay error that is now come over here we have our toggle and we want to put that event on there so see we have a on value changed plus connect that object the scene we go to our player object where the script let's say the player script and we want to be up here in this dynamic bool area okay so that will pass dynamically the value of that what it changes okay so now when I hit play you will notice if I turn it off it press it now false true false true false true false so I can do something with that as it changes note however that it's important to consider what my initial value is so it's not going to report that initial value to user toggle so it's up to me to make sure that the initial value that I set here and the value that I assumed in the script are one and the same you know I could do that through doing the following I could say oh okay public gameobject toggler and here I could look to see what that value is right toddler dot okay before you do this go here unity engine dot UI come on there we go and so here in this initial when I was a toddler dot get component and I want to use this pony this version here and the it is the toggle get that one and I'm gonna look at equals go value or value change note is on is on believe that is the value okay so we're gonna say print during our startup we're gonna print that value say that we hit play and it didn't like it so what did I do wrong mmm it said need to assign the toddler out Wow good here again we want to drag toggle to there do that one or not in clay mode there okay you can see it is true if I come over here and before the game plays I change that to toggle is on two folks play the game it's false okay so you can use that as my initial state and then any time it changes I will get it because I use the is that the on value change so it's false it's true false true false true and so on
Info
Channel: eraserMice Interactive
Views: 23,384
Rating: undefined out of 5
Keywords: unity, toggle, game development, unity ui
Id: zEeE9x9BBjo
Channel Id: undefined
Length: 5min 20sec (320 seconds)
Published: Mon Sep 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.