Creating Components objects and C# objects

So far in this book, you've been taught the basics of writing code to create Components for GameObjects. Incorporating a State Machine into Unity means I have to take you just a little further into the normal C# programming environment.

This will help your game coding in two ways:

  • You will understand what Unity is doing behind the scenes when it creates Component objects in memory
  • Making use of a C# interface will help with the game control logic

Unity creates Components behind the scenes

You already know that a Unity script is really just a file on your hard drive that defines a class. Attaching this script to a GameObject allows Unity to create a Component object in memory when you click on Play.

Unity hides this process of creating a Component object from your view. In my opinion, making things simple by automating processes is great because it saves time; however, it doesn't help you understand how to code.

There are third party products available that try to totally automate the code writing. Once again, this is a good thing but if you find you need to tweak or modify anything to get the exact behavior you demand, you have to get your hands dirty at some point and actually modify or write the code you need.

Well, guess what? If you don't understand how a code works, you're stuck. Learning some C# basic programming skills doesn't require a PhD in Rocket Science. If you truly want to be comfortable using Unity, knowing the basics of C# is required. You don't have to know all kinds of advanced C# coding skills to write scripts.

Look back at the material you learned in the first six chapters of this book. There wasn't anything so earth-shatteringly complicated that made you throw your hands in the air and give up. Sure, there were a bunch of words you had to learn, but everything you learned was just basic, logical steps.

Instantiate a class to create an object

Here's another new word, instantiate.

Not to worry, it's just a word that means create an object from a class. I've been saying create an object throughout this book. Now you have a word for it.

This is what Unity does behind the scenes when a Component is created. The script we've been using is the LearningScript class. When you clicked on Play, Unity instantiated LearningScript to create a LearningScript object which was a Component object of Main Camera.

We're going to leave LearningScript behind and start using StateManager, a part of the State Machine.

Note

Delete LearningScript from your Scripts folder in Unity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.249.198