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

C# inherit Base Class (models)

Discussion in 'Scripting' started by xtoc, Aug 4, 2022.

  1. xtoc

    xtoc

    Joined:
    Jun 3, 2017
    Posts:
    15
    Hi,

    I'm currently finding a solution to set up default model within base class.

    Lets say a list of game objects :

    Code (CSharp):
    1. [System.Serializable]
    2. public class Events {
    3.         public GameObject eventItem;
    4. }
    Then i would create a second class that will use this base class:
    Code (CSharp):
    1. [System.Serializable]
    2. public class EventsSomething : Events {
    3.         public float speed;
    4.         public string label:
    5. }
    Within my Event handle class, i'm using this model:
    Code (CSharp):
    1. private EventsSomthing[] eventsSomething = new EventsSomething[]{};
    I know that the above would not work.
    But the idea is that i want to have all GameObject EventItems by using the GetComponent<Events>() base class to receive those eventItems variables from the model. I don't need those other variables as those are handle within the Master class..., yet those should stay linked with the Gameobject. Thats why i'm looking for this kind of setup.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Well yes, you can't do what you're trying to do (that's if I understand you, I'm struggling here).

    If you want to GetComponent something, that has to exist as a component, or an interface implemented by a component, on a game object.

    You could initialise a component on each of these game objects that references back to their corresponding Events class, perhaps, but that seems like a pain.

    I'd just make this Events a component, and store references to all of those components instead. Referencing gameobjects is often less useful than referencing a specific component, as every component has a .gameObject property.
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    :) Well to be precise, the above would work fine. However the below does not work as spiney already explained. If you want to use GetComponent, your base class needs to be derived from MonoBehaviour and the instances of your classes need to be created either by dragging the script onto a gameobject in the editor, or by using
    gameObject.AddComponen<YourClassType>();


    Note that any component (MonoBehaviours are components) do already have a link to the hosting gameobject. So there would be no need to add a GameObject field to the class (unless you want to reference a completely different gameobject).

    It seems you are a bit sloppy when it comes to singular plural which makes it a bit difficult to understand what you want to do. First of all a class name should be singular unless the class itself represents / contains a collection of another class. Though even then it's more common to add a "List" or "Collection" suffix at the end of the name. So "Events" is misleading since that class represents a single event.

    The next strange thing is that you used
    private EventsSomthing[] eventsSomething
    in your example code but you said you want to use
    GetComponent<Events>()
    to get them all. This is confusing for several reasons. First of all, if you want to get all "Events" instances and store them in an array, the type of the array can not be a more specific type like in your case. A "EventsSomthing" array can only contain that type or any type that is derived from that class. So if you want to store "Events" in your array, you need an "Events" array. GetComponent gets you the first component of a certain type on a given gameobject. So if those classes were actually MonoBehaviours, the question would be what are those gameobjects they are attached to? In order to use GetComponent you already need a reference to the gameobject. Though you can also use
    FindObjectsOfType<Events>()
    (still assuming Events is derived from MonoBehaviour) to get all loaded instances in the scene. This would also get you all "EventsSomthing" instances as well since an "EventsSomthing" instance actually is also an "Events" instance. That's the point of inheritance.

    Inheritance is often compared to biological inheritance and yes, it can explain a lot of concepts. However class inheritance differs from biology that in the real world, even though you are derived from your mom and dad, you as a person you "are not your mom or dad". In biology we only inherit some (actually pretty much 50%) from each parent. In C# a class can only have one base class it is derived from and it always inherits everything from that base class, no exceptions.
     
  4. xtoc

    xtoc

    Joined:
    Jun 3, 2017
    Posts:
    15
    I've include a screenshot as example on what i like to reach.
    The eventbase contains a monobehaviours class already.
    (The model class won't)
     

    Attached Files:

  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    So do you want to know if this structure is possible, or how to do said structure?

    Because you can't look backwards like this without the information being in/on the object you want the information from. OR you check the object in question with an existing collection to see if it matches some some form of predicate.
     
  6. xtoc

    xtoc

    Joined:
    Jun 3, 2017
    Posts:
    15
    My bad, i've deleted the related post.
    I've also included a screenshot.
    The idea is to have one master class to run all events.
    - The reason why each event has their own class, is because each event has his own behaviour.
    - The reason why each event has a base class is because the master engine is deciding which one to run which event.
    - The reason why i want to have the same kind inherent for the variables is because a event can points to multiple gameobjects. But each gameobject has their settings as well that is specific to the related event.