On why to create new GameObjects

First off, great course. Came back after a few months of hiatus and really looking forward to dive into c# .

As an absolute newcomer to programming I do have a bit of trouble following along in this lecture (maybe it doesn’t help that I am not a native speaker ;)).

Why did you add the AdventureScript.cs via a new GameObject and not by rightclicking in the assets area (Create C# Script)? I guess because it would be attached to the Main Camera and not to our GameObject? When does it make sense to add GameObjects and why exactly? I do understand what components do within Gameobjects, but not why and when to create new GameObjects if that makes sense?

2 Likes

Hi Typo,

There are several ways to create a new script, this just demonstrated it. You can, as you say, either directly add it as a component, in doing so the new script will be placed in the root of the assets folder, or, you can right-click in the assets folder, or sub-folder, create the script and then either use the Add Component button, selecting your script, or just drag it across to it.

It wouldn’t, by default, be added to the Camera GameObject, for it to do so, you’d need to select the Camera, and then use Add Component.

A GameObject is effectively just an id. Components tend to add behaviour. So, if you consider your GameObject to be a thing and then add behaviour to it, the thing will be able to do things.

An example might be creating an empty GameObject and naming it Player, you might then add a script component to that called Movement.cs, perhaps another called Health.cs. As you can imagine from the name of these scripts, they could potentially add movement and perhaps health for our player.

You will also see some more generic GameObjects, perhaps a GameSession or a SceneLoader, these again are nothing special by themselves, but the scripts that might be added to them, perhaps GameSession.cs and SceneLoader.cs add behaviour which might keep track of the various game state data items, or in the case of the latter, provide functionality in order that a scene may load the next scene.

Hope this helps :slight_smile:

2 Likes

Yes, that made it clearer. Thank you for the quick response.

1 Like

You’re very welcome. :slight_smile:


See also;

Privacy & Terms