Help with Inheritance problem

Hi,

I have a problem with the inheritance in my project.

I have a script named DynamicEntityScript.
I also have the playerScript with heritate from the DynamicEntityScript

public class PlayerScript : DynamicEntityScript, IDataPersistence

The DynamicEntityClass has a public attribut DynamicEntityData

public class DynamicEntityScript : MonoBehaviour

    {  
       
        public DynamicEntityData dynamicEntityData;
}

On the same model the player Script has a PlayerEntityData wich inheritate from DynamicEntityData.

public class PlayerScript : DynamicEntityScript, IDataPersistence
    {
       
        public PlayerEntityData playerEntityData;
}
 public class PlayerEntityData : DynamicEntityData

I have a problem I can’t solve.

I create a script wich is suppose to be attached to every dynamicEntity.
So I attached it to “natural” dynamicEntity and to my player.

This script look for DynamicEntityScript attached and in both case it find one.

entityScript = this.gameObject.GetComponent<DynamicEntityScript>();

In the case of the player it foud it as a PlayerSCript but I can’t acces to the playerEntityData with.

8568803--1146992--upload_2022-11-7_16-55-27.png

entityScript is a PlayerSCript, and this player Script as a playerEntityData :

wich as some Data :

but when I look on the dynamicEntityData, the system find out it’s a playerEntityData:


but there is no Data :

so I tested to use this Entity as a player :

if (entityScript is PlayerScript)
            {
                PlayerScript scriptTest = entityScript as PlayerScript;
                scriptTest.playerEntityData.refreshData();
            }
            else
            {
                entityScript.dynamicEntityData.refreshData();
            }

in the case I force the entityScript as a playerScript it work but I don’t understand why I can’t jsut manipulate my playerSCript as a DynamicEntitySCript.

I don’t want to have one script for the player and another for dynamicEntity.
Any Idea why i have this problem?

Thanks for your help.

I’m not quite following what you’re describing…

But I have a weird feeling that you think the object stored at ‘dynamicEntityData’ and the one stored at ‘playerEntityData’ are supposed to be the same object… which they are not.

1 Like

Also having trouble understanding the exact problem, but here’s my best guess:
Regarding this part:

You can’t access a variable declared in a subclass (PlayerScript) with a reference of the base class (entityScript in that snippet, which is a DynamicEntityScript), as a base class doesn’t know anything about its subclasses, only the other way around. So you can access the dynamicEntityData field from a variable PlayerScript playerScript = this.gameObject.GetComponent<PlayerScript>();Which is what you arrived at with your last code snippet.

As for the

part - you can, but you’ll only ever have access to public members declared in the DynamicEntityScript class (or more generally speaking at that level of inheritance, i.e. everything in that class and those it inherits from as long as the access modifiers allow it), not in the subclass (i.e. farther down the inheritance tree). For that you will have to cast to the “more specific” subclass type. Hope that helps.

Edit: The part that makes no sense to me is that your IDE appears to recognize the dynamicEntityData field as PlayerEntityData - given the classes are as represented in your post that is impossible (because the only field that actually is of type PlayerEntityData is called differently - playerEntityData). Is there another field called dynamicEntityData in PlayerScript you omitted / are the fields actually named like that?