9 Visual Scripting Mistakes Beginners Make in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to go over 9 mistakes I see beginners make on the unity visual scripting Discord in my scene I want to bake these cupcakes and put frosting on them I have an oven Advanced object with a script called on bake event trigger I also have a bake cupcake script on the Cupcake prefab looking at the oven baking script we have beginner mistake number one which is using Game object find or transform find there are a few issues with using find one is that you can't rename objects without changing all of your scripts another problem is that what your scripts need so in this case the oven depends on having the object to trigger the event on this requirement isn't stated outright it's buried in the graph it's kind of like in a cooking recipe if the ingredients were in the instructions rather than in the list at the top and also because find searches through every item in your hierarchy this could be searching over hundreds of objects which is very slow and we're doing this expensive search over a hundred times a second because it happens every frame and update so before we take a better approach let's look at the next problem mistake number two is that we have a custom event that is being triggered and update custom events are slow events should just be a signal or a notification imagine if you had to hold the start button of your microwave or your stereo for those devices to run the response to the event should be the responsibility of the object receiving it the response should only listen for the event not use the event to run so let's straighten this out if you aren't familiar with scripting parent-child relationships familiarize yourself with and practice those capabilities of the transform component so rather than finding the cupcake I'm going to make the Cupcake A Child of the oven and then this script is going to trigger on bake on all of the children underneath on start we'll do transform get child count and then do a for loop with get child the oven as a parent transform already knows how many children it has and with the child index number it could get each object and Trigger the on bake event on each now let's look at the cupcakes and figure out why they aren't baking their colors should be changing as time goes on I'll move all the cupcakes inside the oven so they get the event and then let's look at the first cupcake first I'll connect up this debug log on the event I'm going to use Game object get name so that when I look in the console I know which cupcake created the log next we have another kind of find transform find let's not use that and just do an object variable of type game object for the cupcake that is this parent object needs the cupcake model so that we can get the material and then change the color on the material each prefab then will have a reference to its own cupcake model then we have the renderer component and we're getting the third material on the component since the cupcake has multiple materials we're getting the material from the renderer because we want the instance of the material we don't want to change the material globally in other words we'll have the color just change for this cupcake and not another cupcake that has the same material and here we are at mistake number four not lurping correctly we have an uncooked dough color and a baked color so we want alert from color a to color B T is the percentage to go from A to B but the script is changing the value by the same percent every frame using a timer node with lerp works very elegantly because the timer node comes with an elapsed percentage going from zero to one and we can plug that right into the T value first I'll plug tick into the material set color because we are going to do that for the duration of the timer every frame and then let's bring in the elapsed percentage into the lerp now we have a variable here called bake speed and I want to change that to bake time since we're using the timer and then we just want to start this timer and with that the cupcakes are baking all right now let's say I have this next step where I want to put frosting on the cupcakes how do we check that all the cupcakes are baked and they're ready to have frosting applied I have a is baked Boolean and rather than have this Boolean be the concern of some other object let's just make it so that the cupcakes themselves switch to another parent when they're baked one is baked is true think of what your scripts are concerned about and if an object can be concerned with itself then overall there are less things responsible and concerned for other things your system is easier to understand and easier to work with and you'll see that in a moment so what I'm going to do then is have the cupcakes be responsible for themselves and switch themselves to another parent when they're baked I'll delete this stuff and when the timer is done we'll set baked to true and then set the parent to the baking complete parent object so I'll make a variable for that and then drag that in and connect it up to the transform set parent now our other prefabs don't have that variable so when we apply this and the prefab override the variable shows up but it doesn't have a value there so we'd have to drag the bacon complete parent to each one of these variables which I don't want to do and actually now that I'm thinking about it I want to do this a little bit differently these cupcakes are prefabs and we may want to spawn them in and when they spawn in they're not going to have a value for this bacon complete parent because that lives in the scene it isn't part of their prefab package the prefab template can only reference things that are a part of the template right now it makes sense to me then for the oven events to have a variable for the baking complete and then it will send the baking complete parent object to the cupcakes so let's make a variable for baking complete parent make it of type transform and then I'll drag in the baking complete object and we're going to pass this along as an argument for the custom event so let's go to the cupcake script and on bake we'll do a set variable of the baking complete parent for the argument coming in and now we have reference to the parent but I don't want this to be an object variable because it's going to be set and we're not setting it ourselves so let's delete it as an object variable and make it a graph variable so picking complete parent and then we need to switch the variable here to a graph variable okay I'll apply the prefab and let's see if this works and there we go we can see that they all switched to the bacon complete parent awesome how do we know then that baking is complete for all objects well we can see that oven events is empty and oven events is already concerned with how many children oven events has so I think that's a great place to check that everything's baked however right now our script on bake event trigger that name exactly describes what the script does let's not add checking children to it because that's another responsibility so we just want to have one script do one thing so I'll make a new script and call it check if no children and then let's do update get child count and see if the count is zero the great thing about this is that since the of an event object only is concerned with the children we could put anything in the oven that needs to be baked let's say that we have cookies that take 100 years to bake the oven doesn't care about what's getting the on bake event or why it takes 100 years it's only concerned with trigging events on the children and whether there's no children beginner mistake number seven is always doing everything on update every frame in this case we don't really need the oven to be checking on the cupcakes 120 times a second so we could just do a cool down node and check it a couple times a second or we could use modulo so that every 20th or 50th frame the oven checks modulo gives us the remainder of the division so every 20th frame the remainder is going to be zero because the frame count is dividing cleanly I'm in the next stage of the game now where the player puts frosting on the cupcake with a simple click camera get main Returns the first camera that's tagged as Main and potentially you're going to have a lot of cameras in your scene so it's good to be specific using get variable is much faster than camera get main so store a reference to the camera or you can use camera get main just on start mistake number nine is always using instantiation when you could just easily turn things on and off instantiation is slow and this way all the parts of the cupcake are a part of the prefab and everything cupcakey is together without needing other assets that are elsewhere in the project folder from the raycast hit we'll get the transform the game object and then trigger on ADD frosting on the Cupcake Prefab game object so the cupcake will have a script for adding the frosting on the Cupcake then here is the add frosting script with the on ADD frosting custom event and then I'm going to make a object variable of type game object and drag in the frosting and then do game object set active to true so that the frosting is turned on when we click on the Cupcake I'll apply the changes to the prefab and this is great because we can also turn other things on like special effects or animations with this event we also might have things that have different kinds of frosting like birthday cakes or Donuts we don't need the frosting controller to have dozens of different kinds of frosting that it has to check to know which one to instantiate we can just do this on ADD frosting event and whatever is out there can respond with its own frosting or sprinkles or whatever alright I hope that was helpful if you'd like to learn more from me there's a link below to my udemy course take care
Info
Channel: Home Mech
Views: 2,441
Rating: undefined out of 5
Keywords:
Id: OIT-SnMiBRI
Channel Id: undefined
Length: 10min 34sec (634 seconds)
Published: Wed Jan 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.