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;
- static vars in a class, the is not in the scene at all.
- DontDestroyOnLoad(gameObject);
Are there other / better methods to keep things in scope when switching scenes?