Is it possible to AddComponent() of a nested class?
public class ParentClass : MonoBehaviour {
void Awake(){
gameObject.AddComponent<ParentClass.NestedClass>();
}
public class NestedClass : MonoBehaviour {
public NestedClass() { Debug.Log(“added”); }
}
}
I saw this post Using AddComponent to add a Sub Class using a String - Questions & Answers - Unity Discussions but it didn’t seem to work for me
You can’t call a nested class from the parent class, that is not how inheritance works.
Also the nested class is deriving from MonoBehaviour not your ParentClass. I have no idea what you are trying to achieve.
I think you need to read up on inheritance. But the key is:
Your base class (parent) derives from MonoBehaviour, and your inherited classes derive from your base class. You can then add your inherited classes to a GameObject as they are derived from your base class, which derives from MonoBehaviour.
Thanks for the reply, this was just a thought experiment. I was trying to see whether it was possible to put all my code under one class in Unity.
Sure you can do what you like, but i like to break down everything into bite size classes that do a specific task.