GetComponent not working in parent class

I have two seperare scripts

public abstract class Enemy : MonoBehaviour {
     public Rigidbody2D rb;
     
void Start() {
rb = GetComponent<Rigidbody2D>():
}

}

and

public Slime : Enemy {
private Rigidbody2D slimeRb;

void Start() {
slimeRb = base.rb;
}
}

When I attached the Slime script to my gameObject, I found out that slimeRb is strangely null because I kept on getting nullException errors

Does anyone know how to fix this? Essentially, I want to create an abstract enemy class that my other enemies script can inherited from.

Your Enemy class gets the rb in Start and the Slime class also gets rb in start, because they both use Start there could be issues with one Start method running before the other and so it would get a null reference sometimes but not neccessarily all the time, it would depend on the order the scripts compile, which you can set, however a better way to fix this is to use Start and Awake, whenever a gameObject is getting a component from itself, you should use Awake, and when it gets a component from another gameObject you should use Start, that way when you try to get the component from the other gameObject it will be assigned.

However, I don’t believe this is your issue, I believe the issue is that because it’s a base class and a child, you are overwriting the Start method of Enemy with Slime and so the Start method of Enemy will never run, the better solution above will fix this, although what you should do is call the base method from the child method like this syntax:

baseClassName :: functionName();

So in the Start method of your Slime class you would put:

Enemy :: Start();

And that will call your Enemy Start method from your Slime Start method.

There seems to be a problem with your script I believe.
It looks like you’re trying to access the rb member of the base Enemy class from the Slime class, but you’re not using the correct syntax. In the Slime class, you should use the base keyword to access members of the base class, like this:

public Slime : Enemy {
    private Rigidbody2D slimeRb;

    void Start() {
        slimeRb = base.rb;
    }
}

However, it’s worth noting that in the Enemy class, you’re setting the value of rb in the Start method, which means that it won’t be initialized until the Start method is called. Since the Slime class is derived from the Enemy class, it will call the Start method of the base class before its own Start method is called, so rb will be null when the Slime class tries to access it.

One way to fix this would be to initialize the rb member in the constructor of the Enemy class, like this:

public abstract class Enemy : MonoBehaviour {
    public Rigidbody2D rb;

    public Enemy() {
        rb = GetComponent<Rigidbody2D>():
    }
}

This way, the rb member will be initialized before the Start method of the Slime class is called, and you should be able to access it without getting a null value.

I hope this helps! Let me know if you have any other questions.