Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Jan 28, 2018 at 11:16 AM · scripting problemmonobehaviour

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Jan 28, 2018 at 06:03 PM 0
Share

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.

avatar image Hamburgert $$anonymous$$ · Jan 29, 2018 at 07:33 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

178 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges