I created a Character class with a constructor that sets a name for character and a Weapon struct containing weapon name and damage in one file. I then created a Character child class, in another file, named Paladin, containing a Weapon type variable and the constructor inherited from Character class that sets a name for the character instance and creates a new Weapon instance. When I make a new Paladin type object, in another script file, using the base constructor, Unity Prints out a CS0103 error, telling me that the name I gave to the Weapon type variable doesn't exist in the current context.
I hit a huge solid brick wall. Please help. I tried everything I can think of.
Character class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character {
public string characterName = "";
public int characterExp = 0;
public Character() {
characterName = "Not assigned";
}
// CONSTRUCTOR SETTING A NEW NAME FOR THE CHARACTER INSTANCE
public Character(string characterName) {
this.characterName = characterName;
}
public virtual void PrintStatsInfo() {
Debug.Log($"Character: {characterName} - {characterExp}.");
}
private void Reset() {
this.characterName = "Not assigned";
this.characterExp = 0;
}
}
Weapon struct:
public struct Weapon {
public string name;
public int damage;
public Weapon(string name, int damage) {
this.name = name;
this.damage = damage;
}
public void PrintWeaponStats() {
Debug.Log($"Weapon: {name} - {damage} DMB.");
}
Paladin class (character child):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Paladin: Character {
public Weapon weapon;
//CONSTRUCTOR INHERITED FROM CHARACTER CLASS
public Paladin(string characterName, Weapon weapon): base(characterName) {
this.weapon = weapon;
}
public override void PrintStatsInfo() {
Debug.Log($"Hey {characterName}, take your {weapon.name}");
}
}
Script file doing the other work:
using System.Collections.Generic;
using UnityEngine;
public class LearningCurve : MonoBehaviour {
//CREATING THE PALADIN INSTANCE USING THE BASE CONSTRUCTOR
Paladin knight = new Paladin("Osip", sword);
void Start() {
knight.PrintStatsInfo();
}
}