0

C++ Developer trying to get started in Unity here.

I've been playing around in Unity and watching some videos / reading the documentation. I have a couple of questions on inheritance and I hoping someone can point me to something helpful.

MonoBehaviour: this seems to be the standard, most scripts will use this, it is attached to a gameobject and is 'generally' destroyed with the scene.

ScriptableObject: allows you to create 'assets' outside of the scene, which have persistence.

: testing has shown that you can create 'normal' classes and Instantiate them inside of inherited classes with 'new'.

Is it possible to inherit from more than one class? I assume so, but what are the rules here?

Can I do:

Baseclass

SubClass : ScriptableObject, Baseclass

OR

Baseclass

SubClass : MonoBehaviour, Baseclass

etc? Or do I have to use the same inheritance everytime? ie.

Baseclass : ScriptableObject

SubClass : ScriptableObject, Baseclass

Also can how do I get a 'ScriptableObject' into a scene? I have managed to do as so:

using UnityEngine;

[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/PlayerData", order = 2)]

public class PlayerData: ScriptableObject
{
    public int maxHealth;
    public PlayerDictionary.PlayerEnum playerEnum;
}

using System.Collections.Generic;
using UnityEngine;

public class PlayerDictionary : MonoBehaviour
{
    [SerializeField] private PlayerData[] playersInGroup;
    private readonly Dictionary<PlayerEnum, PlayerData> playerFinder = new Dictionary<PlayerEnum, PlayerData>();

    private void Awake()
    {
        for (int x = 0; x <= playersInGroup.GetUpperBound(0); x++)
        {
            playerFinder.Add(playersInGroup[x].playerEnum, playersInGroup[x]);
        }
    }

    public PlayerData FindPlayer(PlayerEnum type)
    {
        PlayerData ret = null;
        playerFinder.TryGetValue(type, out ret);
        return ret;
    }

    public enum PlayerEnum
    {
        TILE_TYPE_NONE = 0,
        TILE_TYPE_ENEMY,
        TILE_TYPE_ENEMY_ALT1,
        TILE_TYPE_ENEMY_ALT2
    }
}

One final thing: How do keep things in scope?

I have two ways;

  1. static vars in a class, the is not in the scene at all.
  2. DontDestroyOnLoad(gameObject);

Are there other / better methods to keep things in scope when switching scenes?

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • _"Is it possible to inherit from more than one class? I assume so, but what are the rules here?"_ - no, C# does not allow that. It's nothing to do with Unity. –  Jul 24 '22 at 10:17
  • 1
    Thanks, I knew there was a reason I preferred C++ :) –  Jul 24 '22 at 10:20
  • Haha I'm with you there! :) –  Jul 24 '22 at 10:27
  • Also check out [composition](https://stackoverflow.com/a/178368/585968) as a way to almost get around it. –  Jul 24 '22 at 10:28
  • Finally, [Native plug-ins](https://docs.unity3d.com/Manual/NativePlugins.html) –  Jul 24 '22 at 10:32
  • Please use the correct tags! Note that [`[unityscript]`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a custom JavaScript flavor-like language used in early Unity versions and is **long deprecated** by now. – derHugo Aug 25 '22 at 14:23

1 Answers1

0

Is it possible to inherit from more than one class?

No, you can't inherit from multiple classes, but you can implement multiple interfaces

I have two ways;

  1. static vars in a class, the is not in the scene at all.

  2. DontDestroyOnLoad(gameObject);

This is the preferred way of keeping game objects across all scenes, this can be achieved by the following code

public class SomeClass : MonoBehaviour
{
    public static SomeClass instance;
    // other variables

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    // other methods
}

A bit of explanation here, the first time the scene loads, instance is null. In the Awake() method, we set instance to this, which refers to this script and we set DontDestroyOnLoad(). So when you switch scenes, the gameobject to which this script is attached stays in the hierarchy. But when you reload the same scene, you don't want a duplicate of this gameobject. If you are reloading into this same scene, all the gameobjects get created in which there will also be a gameobject that was set to DontDestroyOnLoad() previously. Now in the newly created gameobject, the script's instance is not null this time, hence it gets destroyed. Finally, there's only one gameobject with this script, that was previously set to DontDestroyOnLoad().

Geeky Quentin
  • 2,469
  • 2
  • 7
  • 28
  • Thank you, I haven't come across interfaces yet, I'll look into it! But singletons are the 'preferred' method, alot of people would debate that haha. Its how I've been doing it so far, but I was hoping to find an alternative. ;) Thanks again. –  Jul 24 '22 at 10:18
  • What about getting scriptables into the scene? Do you know a good video / documention on that? So far I can only do this by adding it to a MonoB as either a reference, or as above using a list. –  Jul 24 '22 at 10:23
  • 1
    _"but you can **inherit** from multiple interfaces"_ - the correct term is _implements_ not inherit. _["An interface defines a contract. **A class or struct that implements an interface** shall adhere to its contract..."](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/interfaces)_. Otherwise good +1 –  Jul 24 '22 at 10:29