Not understanding Inherited Classes (still)

Every example I’ve seen of inherited classes, do not explain how the Child class can function as though it were a class of it’s own. And I’m getting errors on things like transform and position and I’m not understanding why these aren’t able to be called while that Script is on the GameObject itself. For my situation I had public class Tree : Monobehavior and had it working just fine. However constantly calling from my handler script:

if (workLoad.name == "Tree1")
{
     workLoad.GetComponent<Tree>().attacker = gameObject;
}
if (workLoad.name == "Log")
{
    workLoad.GetComponent<Log>().carrier = gameObject;
}

Get’s to be a little numbing and messy. So I tired to setup a RawResources Base class, and a Resources class, to handle the many many objects/items I plan to use. And this one block in particular(Tree class) lit up like a christmas tree with errors:

void WhenDie()
    {
        log.transform.SetParent(null, false);
        log.transform.forward = attacker.transform.forward;
        log.transform.position = transform.position;
        log.transform.Rotate(Vector3.right * 5);
        log.SetActive(true);
        Destroy(this.gameObject);
    }

It’s constantly saying that transform doesn’t exist, and after applying private object transform; as one answer I found showed to put into the Tree class, then position is saying it doesn’t exist…

I’m sorry if this is a redundant question, but I truly cannot find an answer to why I’m getting these errors, or even why I should attempted using Inherited Classes in the first place. Can anyone help me by explaining why these error’s are happening, and possibly give me a clue to how to make it all work?

I should have shown that the RawResources class looked like this public class RawResources because of all the examples I watched of Inheriting showed that you don’t use MonoBehaviour the Parent class. But in the Instance of actually using Object Oriented Programming in Unity, my RawResources class should have looked like this

 public class RawResources : MonoBehaviour 

Since everything I was calling transform position Destroy(obj) all are inherited from the MonoBehaviour class. So technically I’ve been using Inherited classes all along, lol… So with reworking the handler script:

if (workLoad.name == "Tree1")
{
    workLoad.GetComponent<RawResources>().attacker = gameObject;
}

and then RawResources:

public class RawResources : MonoBehaviour
{
    public GameObject attacker;
}

and finally the meat and potatoes:

public class Tree01 : RawResources
{
    public GameObject log;

    public float health = 100f; 

    void Start()
    {
        
    }

    
    void Update()
    {
        if (health <= 0) { WhenDie(); }
    }

    void WhenDie()
    {
        log.transform.SetParent(null, false);
        log.transform.forward = attacker.transform.forward;
        log.transform.position = transform.position;
        log.transform.Rotate(Vector3.right * 5);
        log.SetActive(true);
        Destroy(gameObject);
    }

Everything works as though it should… Finally! I’m just not sure why every example and tutorial I watched showed not using MonoBehaviour for Inheriting in Unity… This question will never be known… But, atleast the answer finally is :slight_smile: