Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How much performance overhead does inheriting from monobehaviour create?

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, Dec 7, 2014.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    I have a very small data storage class, it has no functions whatsoever but it would be very handy to drag it onto components in-game and locate it with object.GetComponent<DataClass>(). However, I'm wondering how much extra overhead would making it a monobehaviour add on? I know each instance would need to have its Awake, Update, and LateUpdate checked, and when I'm looking at having potentially thousands of instances of this class, it could add up a bit- am I being paranoid here, or is it worth finding an implementation that doesn't require attaching my class to components in-game?
     
  2. psk-psk6

    psk-psk6

    Joined:
    Apr 16, 2014
    Posts:
    14
    Update DataClass like this
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. public class DataClass  {
    6. public static List<DataClass> list;
    7. public DataClass(){
    8. list.Add(this);
    9. }
    10. }
    Use the static variable list to get all the objects, this will also save u the time for calling object.GetComponen<>()
     
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    Oh that's interesting, I hadn't thought about making the list static- this may be a stupid question, as I'm a pretty utter neophyte when it comes to programming, but the data manager class is the only component in the whole world that will ever need access to this list once it's formed. Because of that, isn't it technically better design to keep the list local to the manager and run all changes to that list through a data handling method inside the manager, instead of letting everyone and their uncle modify a single static list?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    If it doesn't have any Awake, Update, etc., then there's nothing to check. Code only has overhead if it actually runs.

    --Eric
     
    eterlan likes this.
  5. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    Oh, that's very useful to know! Thank you very much :)
     
  6. psk-psk6

    psk-psk6

    Joined:
    Apr 16, 2014
    Posts:
    14
    yep
     
  7. psk-psk6

    psk-psk6

    Joined:
    Apr 16, 2014
    Posts:
    14
    That being said it will definitely occupy memory for all the transform components attached to it and also all the methods that are being called in transform like Update() FixedUpdate() Awake() etc
     
    eterlan likes this.