Search code examples
c#unity-game-enginegame-enginedevelopment-environment

Unity 5.6 and creating objects with C# classes


I've been trying out Unity, so far I've dedicated a lot of time making up a character class with stats and feats just like in Dungeons and Dragons, so far they all work well, but now it comes to me the desire to create gear for such character.

I've seen you can create objects from the prefab section and then add script snippets to change their behavior or even their appearance, but alas it just feels odd when I'm used to make classes from scratch to alter an already existing class of which I barely have any knowledge on how it works.

Thus, I could just simply go into the script folder and create a new C# script with all the values that will be used in general, for example, all armors have an armor class and weight, thus I could make a general class that has all the properties of an Object called Item then inherits its properties to Armor, Weapons and Consumables, and each of them can be then further specialized into more specific objects, now the real question comes:

Should it be better to create a class that holds specific attributes, for example a class called Iron sword, which inherits from weapons and from items down to gameobject, or should I make only one class called weapon that holds all values and names inside lists or arrays? And then... how do I instantiate my scripts without the need of creating one object in the game that when deleted it also removes the script from the scene?

Sorry for so much info, but I'm just a bit overwhelmed by all the things you can do and can't do with Unity.


Solution

  • Unity is intended to be used with component architecture, not object-oriented design. This means, mostly you will be creating GameObjects in the scene and then attach scripts to them, adding behaviour or data.

    Data can also be stored in ScriptableObject instances in the project, or in any other asset like a text file or texture.

    Since there is no main function or any specific program start in Unity, you will have to pick a root manager GameObject in your first scene, which has a MonoBehaviour script and one of the Unity callbacks which get triggered at the beginning (e.g. Awake or Start). This script now can work like your main function in a regular app.

    If you really want to, you could build your entire game from here with "normal" C# classes. They all start at this once MonoBehaviour, but other than that, you could write everything "top down". For example: Instantiate a Game class, which then loads a text file and instantiates GameObjects, assembles all their components, until the visual representation is complete. However, this way you wouldn't make use of all the features Unity provides.

    The Unity way would be to build individual GameObjects from components, which mostly function independently from one another. A human player avatar might consist of PlayerInput, Renderer, WeaponBehaviour, HealthComponent, etc.

    Nobody can tell you what design is best for your game, but both directions are possible. For starters, I'd suggest to create an abstract Weapon base class MonoBehaviour and then for each type create a subclass and put it on a GameObject. This can do stuff like access other components, receive collider trigger messages and store data.

    Maybe you realize, that all weapons do a similar thing. Then you could have a WeaponBehaviour which works like a hub/controller on your player GameObject. It then has references to a data file which contains stats, as well as references to the model, materials, sound, etc.