Unity Events Explained

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so today we're going to be talking about something called unity events and unit event eloise do a series of actions which would previously have to really hard-coded to script now you can see unity events on things like buttons salt on buttons on UI you can see the on click and you can see there's a list which is currently empty you can click on log Plus and you can add a specific runtime event to the list now if you've not got a UI you want to be able to reference this in the script so let me give you an example I've got a script here that I've gotten a trigger event so when I walk into the box collider which is orange I just set an object equal to true so as you can see in my play mode at the bottom I walk into it and that is equal to true now the thing is if we wanted to do so many more actions we could you know write that into this script and we could just do whatever but the problem with that is it's always gonna be hard code to this script and if we've got this on this particular script could be on multiple game objects even we might not want the same functionality on each game object so we might want to do different events on different types of objects that we have this on so what we can do is we can start by using an end space at the top saying using unity engine dot events with a semicolon so now to reference the actual unity event itself we can write at the top of the class square brackets serialize filled private unity event and we can specify the name of the event which would be we could say is my trigger and then what we can specify is my trigger dot invoke which invokes the actual action that we're going to do and this allows us to do multiple actions based on this so now what we can do is we can go back in the inspector take a look at our object and you can see that my event now has a bunch of actions that we can add to it so we can just we can take the box and we can specify actions that it will use so let's say that we add an action to it so we can act and action at runtime so really I've got another class here which is just called cube appear so we can write here is we can just write a public void and we can just call this activate cube as a method that we're going to use and in this class we can just say that serialize field private game object and then my cube and we can specify that my cube dots active is true so we're doing it in this script so we can have I could have 10 other scripts which do ten different things or a script which is now in this case we could have another public void which is you know activate sphere for instance and we could have some code in here which is we'll just rare quick debug line as our example and what we could do in our specific actions we can go back on our whatever object we want to hold this script on so for instance we could just create a new empty game object reset the transforms and we just call this something like our cube manager which is something that you're just going to control the logic that we have on it and the the cube that we want to make appear is this one here so we can go back to our trigger drag our cube manager onto that we can say that we want to go to the cube appear script and we want to do the actual method which is activate cube if we want to do something again we can add the same thing or it could be a different object with a different script and we can just reference and say we want to play activate this sphere so we've got two different actions on one trigger and like I said we could duplicate this trigger here oh now this trigger has a certain type of actions that this trigger here where you can get rid of one of the actions it doesn't need to have this action so we can just split this up and do different use cases so we don't need to use it all of the time so I'll just get rid of this now and you can pet we can press play and when we're in the game we expect to walk into that trigger and you would see the red thing appear and we had the debug which was make the sphere - so it was just a simple way to reference with the namespace of events reference the unity event and then just use invoke to reference the actual event are the action that we're going to take and then within your object you can reference as many actions as you want on a per object basis and specify what type of method you want to be able to execute so this just makes it easy to do refined events and different actions really easily so hopefully this helped you well and added another tool to your arsenal so be sure to check out my patreon my discord and all my great assets on the unity star so thanks very much for watching don't forget to Like comment subscribe Cheers
Info
Channel: SpeedTutor
Views: 45,924
Rating: undefined out of 5
Keywords: unity events tutorial, unity events, unityevents, event unity, unity events brackeys, unity events and delegates, unity eventsystem, how to use unity events, creating unity events, unity event tutorial, create unity event, unity event speed tutor, unity event c#, speed tutor unityevent, unityevents speed tutor, creating a unityevent, unity event animation, unity event brackeys, how to use unity events unity, unity events for unity, unity tutorial, unity3d, unity, c# tutorials
Id: TWxXD-UpvSg
Channel Id: undefined
Length: 5min 8sec (308 seconds)
Published: Thu Jul 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.