What is the best way to avoid or reduce the amount of monobehaviours in your scripts?
Hiya devs,
Basically as the question states. What is the best way to avoid or reduce monobehaviours in your game? I have a 3D endless runner, well, flyer, where everything is object pooled to save on constant instantiation. Although, I am finding I have a lot of scripts for the game, and each has a Monobehviour inheriting.
I have recently started to learn about Scriptable objects, but unsure how these can help me in my game.
Thank you for any answers. :) Have a great Sunday too.
Answer by Hamburgert · Jan 28, 2018 at 05:22 PM
I also had this question recently, because I'm still pretty new to gamedeving.
One way I'm currently trying out is to have 1 main monobehaviour, "Monogame", in an empty gameobject I call "Core". I then let my Monogame initiate and call my own update methods in all my static scripts. These static scripts are not Monobehaviour, just "plain" C# classes.
This means that most of my core game code is actually not Monobehaviour, but various static "managers". Doing this, I found that I didn't have to rely on MonoBehaviours for everything any longer. Any Monbehaviours I have on gameobjects now are mostly to capture collisions, but I'm sure they are useful for a lot more if used correctly.
My monogame might look something like this (simplified):
public class Monogame: Monobehaviour{
void Start(){
// Prepare references and variables as necessary
PlayerCharacter.Init();
AIHandler.Init();
MapHandler.Init();
}
// Update is called once per frame
void Update () {
// Input to movement for the players character
PlayerCharacter.Update();
// Move entities
AIHandler.Update();
// Load new maps
MapHandler.Update();
}
}
How do you manage with creating object references that are used by Unity and inherit from $$anonymous$$onobehaviour if you only have one main script that calls most of the methods in the other scripts?
I do the same, as in using one main script with an Update method. But I the other scripts it calls, they use Transforms, Vectors, etc.
It differs with each case, but e.g. AIHandler will usually hold all my class Entity as a list or array. Then each Entity-object will reference its individual Transform. Entity is also not a monobehaviour. $$anonymous$$anipulation is done while AIHandler iterates over Entity objects each update for smooth movement. $$anonymous$$y idea is that class Entity can manage positioning, movement, etc, without actually having a live Transform, but can then spawn at any moment and just pretend it was always in that position, doing exactly this thing. This, of course, will only work if you do not rely on colliders.
Your answer
Follow this Question
Related Questions
AddComponent fails when passing in type, works when passing in name 0 Answers
Multiple PowerUps (One ItemBox) 0 Answers
Can't add script to anything. 1 Answer
Start() and Awake() not called on MonoBehaviour 1 Answer
accessing a script using a variable with getComponent, then accessing a variable inside that script 1 Answer