0

My question probably comes from a bad understanding of inheritance and composition. I have a couple of classes with inheritance BasePlayerClass=>SpaceShipClass=>SpaceShipObject. I dont understand how to call the instantiated class Player from another classes, so that it saves its properties and doesn't get the null value.

public class CreatePlayer : MonoBehaviour
{


    public Sprite SCALPEL, WARPER, ORION;
    public SpriteRenderer SelectedCharacterSprite;
    public Skill_Slot newSkillslot;

    private readonly string SelectedCharacter = null;





public void Start()
{
  PlayerCreation();

  newSkillslot.CreateSkillSlots();

}

public void PlayerCreation()
{
BasePlayerClass player = new BasePlayerClass();
SelectedCharacterSprite = this.GetComponent<SpriteRenderer>();
    int getCharacter = PlayerPrefs.GetInt(SelectedCharacter);

        switch(getCharacter){

            case 0:
            SelectedCharacterSprite.sprite = SCALPEL;
            player.SpaceshipClass = new ScalpelSpaceship();
            break;
            case 1:
            SelectedCharacterSprite.sprite = WARPER;
            player.SpaceshipClass = new WarperSpaceship();
            break;
            case 2:
            SelectedCharacterSprite.sprite = ORION;
            player.SpaceshipClass = new OrionSpaceship();
            break;

        }
}
}





public class Skill_Slot : MonoBehaviour
{
   public Skill SignedSkill;
   private Image qImg;
   private Image wImg;
   private Image eImg;



    public GameObject qSkillSlot;  
    public GameObject wSkillSlot;
    public GameObject eSkillSlot;

    public BasePlayerClass player;







   public void CreateSkillSlots()
   {
      Debug.Log(player.SpaceshipClass);
      qImg = qSkillSlot.GetComponent<Image>();
      wImg = wSkillSlot.GetComponent<Image>();
      SignedSkill = player.SpaceshipClass.QSkill;

      switch(player.SpaceshipClass.QSkill.SkillIndex){
        case 1:
        qImg.sprite = Resources.Load<Sprite>("BasicAttack");
        SignedSkill = player.SpaceshipClass.QSkill;
        break;
        case 2:
        qImg.sprite = Resources.Load<Sprite>("OrionMove");
        break;
        case 3:

        break;

   }
   switch(player.SpaceshipClass.WSkill.SkillIndex){
        case 1:
        wImg.sprite = Resources.Load<Sprite>("BasicAttack");
        break;
        case 2:
        wImg.sprite = Resources.Load<Sprite>("OrionMove");
        SignedSkill = player.SpaceshipClass.WSkill;
        break;
        case 3:

        break;

   }
}
}
SelfTitled
  • 33
  • 5

1 Answers1

0

Well, I'm not sure what is your desired action, but what do you want by calling PlayerCreation()?

It seems like, if there are no side-effect in your constructor of BasePlayerClass and SpaceShipObject, function PlayerCreation() does literally nothing. You just declared local variable Player, and do something with it, and the scope ends. After the function call, GC will collect that Player local variable, since there is no viable way to access it. Nothing to do with Inheritance until here.

Which means, Player of TestClass and the local variable Player in PlayerCreation() is entirely, totally, definitely different thing. Again, I'm not sure what is your desired behavior, but since it seems you are new to C#, take a look at below code snip:

public class CreatePlayer : MonoBehaviour
{
    public TestClass newTest;

    public void Start(){
        newTest = new TestClass();  // Come on, You have to create an instance to use class!

        newTest.Player = PlayerCreation(); // And now newTest will call the function
        newTest.CallPlayerClass();         // with the return value of PlayerCreation().
    }

    public BasePlayerClass PlayerCreation(){
        BasePlayerClass player = new BasePlayerClass();  // BTW, for local variables, 
                                                         // using camelCase is recommended.
        Player.SpaceshipClass = new SpaceShipObject();
        return player;  // You have to return the local variable to use it!
    }
}

In the above example, you made a new instance of TestClass class, and assigned it's field Player with the return value of PlayerCreation() value.

I strongly suggest you to analyze this code on your own, and take a look of OOP programming. Even until here, your problem had nothing to do with Inheritance.

MyBug18
  • 2,135
  • 2
  • 11
  • 25
  • I edited my post, to be more specific. I need to make the player variable global, so that I can use it in other classes and methods. And its properties should be defined in PlayerCreation(). – SelfTitled Apr 17 '20 at 01:57
  • Then you can define `static` variable, like `public static BasePlayerClass Player`, and assign it's value in `PlayerCreation()` function. – MyBug18 Apr 17 '20 at 02:10