What is the DIFFERENCE between Transform and GameObject? (Unity Tutorial for Beginners)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome i'm your code monkey here's a super quick video on something that some people find quite confusing which is what is the difference between a game object or a transform now where i see this question pop up is whenever i have some video where i call in century to create some sort of prefab instance and usually as a personal preference i tend to use transform references for the prefabs instead of game objects and for some people that can be confusing to see if you've always learned to reference your prefabs only as game objects so as an example here i've got some super simple code i've got a prefab reference of type transform it's also a serialized field so it's exposed in the editor now if you don't know about serialized field if you normally just expose it as public then go watch my video on why you should not make everything public so here i have this simple reference to my prefab as a type transform and in the editor here is my script with that field and i can simply drag the prefab reference then here on the code just asking for some input and calling instantiate on that prefab so if we run this i just press the button and yep there you go my object is incentive by the way this object is a really nice money clip from the cinti polygon heist pack it's a great pack with lots of money and lots of stealth lots of heists so if you want to get it check it out with a link description okay so with this the object was spawned so everything works perfectly fine however you might have learned to use game objects so if you go up here and you change the type from transform into game object so if you do this all of the code works just the same and here in the editor let's assign the exact same reference there it is and if we try then press on t and if there you go everything still works exactly the same however don't be confused this does not mean that a game object and transform is the exact same thing that is not true those are two unique different types if you grab this prefab and you try to cast it back into a transform if you try doing this then nope you have an error you cannot convert the type game object directly into a transform however at the same time despite being two different types they're also pretty much interchangeable the reason for that is because every single game object always has to have a transform component if you're in the editor you right click let's create an empty new game object and in the inspector you can see yep it comes with a transform component if you right click on the transform you can't even find the option to remove it whereas if you add a random component you right click you can remove it but transform nope cannot remove it and conversely every transform always has to be attached to a game object if you go up here and you just try doing new transform then nope that does not work unity does not let you do this so game objects and transforms they are two different types but they always need to work with one another so that's why they end up pretty much interchangeable anytime you have a game object reference you can grab the transform by just going in there and grab the transform and on the other hand if this was a transform reference you can go into the transform and grab the game object reference so from one you can always get the other one now the main difference is over here when calling instantiate if you have a transform reference and you call instantiate and pass in a transform then you can see that this returns another transform whereas if up here you change this into a game object if you do that and you can't send sheet now this one returns a game object now for me usually when i instantiate something i like to move it or scale it or do anything like that so if i make this directly uptype transform then this one is going to return a transform so i can just grab the transform for the prefab transform i can grab that one and then directly just access the position and set it to something so i can do this directly it's super easy whereas if i change this into a game object then now this one instead of returning a transform it returns a game object so let's rename this to the prefab game object and now in this if i did want to modify the game object right after instantiating then over here i have to access the game object then access the transform and then i can move it so that's really the main difference with using a transformer and why i intend to use them if you use a transform for the prefab then instantiate returns the transform and over here you can avoid having to access the transform from the game object so you just avoid one extra access call and of course like i said all transforms are always attached to game objects so even if you use a transform reference so even if you use this if you want to access the game object just access transform.gameobject so for me personally since i tend to work more directly with transforms as opposed to game objects like i said usually i like to move the object change the scale rotation and so on so because of that i tend to use my prefab references as transforms but for you perhaps if you tend to work more with game objects and transforms like perhaps usually right after calling instantiate after that perhaps you tend to call the gameobject.setactive if normally this is the type of thing in order to enable or disable a certain instantiated game object if you tend to do that often then it might be more useful to define all of your prefabs as game objects so that's just a quick explainer on transform versus game objects when it comes to prefabs you can use whatever type you prefer and personally i tend to prefer transforms also one more note on transforms versus game objects if you want to destroy an object and you try to destroy the transform so appear called destroy on this prefab transform if you try to run this code then nope there's our issue we cannot destroy the transform component again like i mentioned previously you cannot have a game object without a transform so if you want to destroy the transform the only thing that you need to do is you need to destroy the actual game object if you do this and you press the button yep the game object is spawned and destroyed alright so i hope this video helps clarify why sometimes you might see transform prefabs and other times you might see game objects if you found this video because you're commonly new to unity check out my video doing a quick overview of how the engine works and if you're just past the complete beginner stage and you want to learn more about the engine then check out my turn turn-based strategy course i designed this specifically to help guide you and take you from beginner to advanced even if you are not interested in that specific genre you will still learn a ton in that course i assume you know the absolute basics and then i help you take the leap to the next level by making a more complex game using things like c sharp generics using delegates in machine ai and all of that while focusing on writing good clean code so if you want to take your game dev skills to the next level then check out the course alright hope that's useful check out these videos to learn some more thanks to these awesome patreon supporters for making these videos possible thank you for watching and i'll see you next time [Music]
Info
Channel: Code Monkey
Views: 35,423
Rating: undefined out of 5
Keywords: unity game object transform, game object vs transform, unity transform gameobject, unity gameobject, unity transform, code monkey, unity for beginners 2022, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming, unity tutorials, how to make a game
Id: B-dVf9wUEbg
Channel Id: undefined
Length: 6min 35sec (395 seconds)
Published: Thu Jul 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.