Home > front end >  Unity Inheritance & scope
Unity Inheritance & scope

Time:07-24

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?

CodePudding user response:

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().

  • Related