Base Class Inheritance issue

Fairly new to scripting in C# and I’ve been digging around to find an answer at least similar to what I’m experiencing and haven’t had any luck or haven’t found the answer in layman’s terms that I understand fully.

I’m trying to create character classes. I want to have 3 of them. I want to have a BaseCharacterClass which will hold the stats of a character (health, strength, etc.) and then have class scripts (BaseWarriorClass) which will inherit those stats and begin to fill out some of the code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseCharacterClass
{   
    public string characterClassName;
    public string characterClassDescription;
    //stats
    public int stamina;
    public int endurance;
    public int strength;
    public int intellect;

    public string CharacterClassName
    {
        get { return characterClassName; }
        set { characterClassName = value; }
    }
    public string CharacterClassDescription
    {
        get { return characterClassDescription; }
        set { characterClassDescription = value; }
    }
    public int Stamina
    {
        get { return stamina; }
        set { stamina = value; }
    }
    public int Endurance
    {
        get { return endurance; }
        set { endurance = value; }
    }
    public int Strength
    {
        get { return strength; }
        set { strength = value; }
    }
    public int Intellect
    {
        get { return intellect; }
        set { intellect = value; }
    }
}

That makes sense to me so far.
However, when I try and make a BaseWarriorClass script, it doesn’t inherit anything.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseWarriorClass : BaseCharacterClass
{
    public void WarriorClass()
    {
        CharaterClassName = "Warrior";
        CharacterClassDescription = "Strong and powerful hero!";
        Stamina = 15;
        Endurance = 12;
        Strength = 14;
        Intellect = 10;
    }
}

This gives me the error:
"
Assets\Scripts\Character Scripts\BaseWarriorClass.cs(9,9): error CS0103: The name ‘CharaterClassName’ does not exist in the current context
"
which to me means, it’s not actually inheriting from the BaseCharacterClass.

I used to have this working before and it worked 100% fine a few months ago, but now it doesn’t seem to work properly. Did Unity remove this ability?
I appreciate any and all help I receive!

Check your spelling on the class name. :slight_smile:

Hahaha. Okay so I fixed the spelling and the Unity issue went away.

But now say if I want to make a Rogue script and inherit from BaseCharacterClass.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseRogueClass : BaseCharacterClass

When I start to type BaseCharacterClass, it doesn’t populate. And as I’m typing in information that should be inherited from it, it doesn’t populate either

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseRogueClass : BaseCharacterClass
{
    public BaseRogueClass(){
        CharacterClassName = "Rogue";
        CharacterClassDescription = "Stealthy and powerful hero!";
    }
}

And so I put together a test script to see if it would print any information to the GUI it doesn’t return anything.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestGUI : MonoBehaviour
{
    private BaseCharacterClass class1 = new BaseMageClass();
    private BaseCharacterClass class2 = new BaseWarriorClass();

    void Start()
    {  
    }
    void Update()
    {      
    }

    void OnGui()
    {
        GUILayout.Label(class1.CharacterClassName);
        GUILayout.Label(class1.CharacterClassDescription);
        GUILayout.Label(class2.CharacterClassName);
        GUILayout.Label(class2.CharacterClassDescription);
    }
}

You my good sir, must pay much closer attention to your spelling and your capitalization:

Also, this may help you with intellisense problems:

Also, try update the VSCode package inside of Unity: Window → Package Manager → Search for Visual Studio Code Editor → Press the Update button

Also, this: No suggestions in Vscode

2 Likes

I’ve done all those steps but my BaseRogueClass script still doesn’t seem to be inheriting anything from my BaseCharacterClass script. I’m stumped!

EDIT: What really has me stumped is I have code written like this from a few months ago and it was working fine with no issues. The child classes would inherit from a parent class and begin to auto populate the words as I typed them. Written the exact same way in fact. However, now even the old code doesn’t seem to be inheriting.

Update:
Turns out if I use Visual Studio 2017, this works as intended. For what ever reason, when I’m using Visual Studio 2019 or Visual Studio Code (C# extension v1.22.0 - v1.23.2) my BaseRogueClass doesn’t inherit anything from BaseCharacterClass.

Does anyone know why this would be the case?
Is there a fix for VS2019 that I could use or should I go back to VS2017?

What do you mean “doesnt populate anything”? Do you mean when you are typing it doesnt autocomplete your variables for you? If you type it out anyway, does it show you an error, or does it compile?

If it compiles it is inheriting from your base class. Sounds like an intelisense issue you’d have to google for. Im sure you cant be the only one with the issue.

Try “Unity Visual Studio 2019 Intellisense Problem” and general variations of that.

1 Like