-1

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();
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
Strange..
  • 3
  • 1
  • 1
    So in the expression `new Paladin("Osip", sword)`, where do you expect the variable `sword` to come from? (Note that you haven't shown any such variable anywhere in the code - not even somewhat that you wouldn't be able to access it. It's just not there at all.) – Jon Skeet Jun 20 '21 at 11:36
  • Please use the correct tags! Note that [`unityscript`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a JavaScript flavor like custom language used in early Unity versions and is long **deprecated** by now! [`[csharpscript]`](https://stackoverflow.com/tags/csharpscript/info) is a CLR based scripting system which uses the C# programming language. This is not to be used for just any questions about `c#` ;) – derHugo Jun 20 '21 at 12:12
  • @JonSkeet, I already tried to create a weapon type variable to pass into the constructor but Unity logged a cs0236 error, stating that "A field initializer cannot reference the non-static field, method, or property 'ScriptFileName.sword' ". – Strange.. Jun 20 '21 at 12:19
  • Right, so you'd need to put the initialization code in a constructor, but still declare the fields outside the constructor. Did you try doing that? (It's always worth showing what you've tried in your question - so if you've tried creating an appropriate variable, please include that in the question...) – Jon Skeet Jun 20 '21 at 13:14

1 Answers1

0

You have to create variable, before passing it down to constructor:

Weapon sword = new Weapon("Name", 1); // 1 as a damage
Paladin knight = new Paladin("Osip", sword);
Timur Umerov
  • 473
  • 3
  • 8
  • I already created a variable to pass into the constructor as follows: `Weapon sword = new Weapon("sword", 1); Paladin knight = new Paladin("Osip", sword);` But Unity logged the error cs0236, stating "that A field initializer cannot reference the non-static field, method, or property 'ScriptFileName.sword' ". – Strange.. Jun 20 '21 at 12:10
  • @Strange.. It's not in the code Example you posted ... But this would make your question a duplicate of [A field initializer cannot reference the nonstatic field, method, or property](https://stackoverflow.com/questions/14439231/a-field-initializer-cannot-reference-the-nonstatic-field-method-or-property) .. why not simply create both instances in `Awake` or `Start` where you are not in a static environment anymore? – derHugo Jun 20 '21 at 12:14
  • Just change var with `Weapon`, since you cant use var in field declarations – Timur Umerov Jun 20 '21 at 12:20
  • And @derHugo makes the right point, you should move it inside some method – Timur Umerov Jun 20 '21 at 12:21
  • @derHugo, you're right. I moved it inside Start and it is now fixed. Thanks for your help! – Strange.. Jun 20 '21 at 12:24