How do i script? Differences to regular game programming

I have been programming games for a few years in java and c++ and just picked up Unity a few days ago and have some problems coming around the scripting in a good way.
I know you add scripts to objects and these scripts have functions that are called on initialization, updates, and so on. But what I don’t understand is how i connect these with everything else. How am I supposed to think when I do this? If I want a game that uses tiles, how and where do I create the tile array? How do I get info from it to the objects? To the game world? And so on. Any good tips on how to think? This was just an example.

The way I’m doing it, I also write standard classes that I then instantiate at runtime. This includes my singleton App class, that manages the general state of things. I believe it was this guide that helped me wrap my head around this approach.

Yeah, you do need to adjust your thinking a bit to make the best use of Unity’s component-based architecture. OK, so, try this on:

The main data structure in Unity is the “Scene”, which is a collection of a bunch of GameObjects.

A GameObject is basically just a container for a bunch of components, which are little scripts derived from Component. Most of the components you write are derived from MonoBehaviour, which is a Component subclass that defines a bunch of standard callbacks for the game engine: Awake, Start, Update, etc. Another extremely important component that almost every GameObject has is Transform, which does two things: it defines where the object is (and how it’s oriented) in the game world, and it also defines a parent/child hierarchy in the scene. Sometimes we use that for virtual objects made of parts (a robot made up of a torso, two legs, etc.), and sometimes we use it just for organization (putting all the explosion effects inside a “ExplosionFX” GameObject just to keep the scene hierarchy neat).

OK. So, on each frame of the game, the game engine zips over all the GameObjects, and basically updates each of the components. That means various things: a Rigidbody component hooks into the physics engine, so Unity does the physics simulation; an AudioSource component hooks into the audio pipeline, etc. And any MonoBehaviour-derived component gets its Update and FixedUpdate method called, as well as other callbacks at the appropriate times.

So, once you’ve got a grasp on GameObject, Component, Transform, and MonoBehaviour, you know most of what you need to know.

But then you asked: what do I do with all this? In particular, where do you store more global-ish data, and how scripts talk to each other?

Well, the C# in Unity is just ordinary (if somewhat out of date) C#, so you can make use of static methods, singleton patterns, etc. if you like.

Or, for slightly more Unity-ish approach, you can make a MonoBehaviour-derived class that provides your global data (list of tiles or whatever). Stick this on a GameObject in your scene, and fill its public data out using the inspector. Then, from other code, find that special object by name, or by tag, or by having a reference to it.

When scripts need to access each other in other cases, there are again various ways to do it: one component might have a public property whose type is some other script, and then you hook it up in the scene; or maybe you can check for a certain component on an object you’ve collided with (which you can find out in the collision handler), or you can use some sort of event system, like UnityEvent. (Check out my 15-minute demo, and see if you can follow along at home — I bet you’ll pick up some handy insights.)

HTH,

  • Joe
5 Likes

@JoeStrout , very nice thorough introduction there.

I just want to add a minor thing: Please don’t use public class members in a MonoBehaviour to make them visible in the Inspector window. Instead, keep those members private and use the [SerializeField] attribute.

1 Like

JoeStrout gave you a very thorough explanation of how scripts in Unity work. Beyond that, I might suggest checking out my book Unity in Action. It teaches how to program games in Unity to people who already know how to program but are new to Unity.

Unity is very component based. Scripts are added to GameObjects as Components and you access other components by using the foo.GetComponent() method and invoke methods or affect variables on those components.