Hey there,
I have my classes:
class UISelectedCharacter : MonoBehaviour {
/* Some serialized fields */
}
public class UICurrentCharacter : UISelectedCharacter {
}
I want to get them via:
public class UISystem {
private UISelectedCharacter selectedCharacter;
private UICurrentCharacter currentCharacter;
public UISystem () {
selectedCharacter = GameObject.FindObjectOfType<UISelectedCharacter>();
currentCharacter = GameObject.FindObjectOfType<UICurrentCharacter>();
}
}
selectedCharacter is working fine. However, currentCharacters keeps being null. Both scripts are attached to gameobjects in the scene and both are active. Both serialized field are properly shown in the Inspector as well.
I tried to do the same with FindObjectsOfType in both cases. My UICurrentCharacter is present in both of the returned arrays(which is the intended way and also the correct one according to the docs) However, FindObjectOfType still won’t find UICurrentCharacter.
Am I missing something? Are there any issues relating to Inheritance and FindObjectOfType? It seems weird that FindObjectsOfType and FindObjectOfType are working differently here.
Sure I could work around it, but I’m interested in why it doesn’t work.
Is the one that's not working, is it enabled in the scene?
– CynikalCan you try: GameObject.FindObjectOfType<typeof(UICurrentCharacter)>();
– jmgek