简体   繁体   English

从继承的 class、C# unity 创建实例时出现问题

[英]Problem creating an instance from inherited class, C# unity

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.我创建了一个 Character class,其构造函数在一个文件中设置了角色名称和包含武器名称和伤害的 Weapon 结构。 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.然后,我在另一个名为 Paladin 的文件中创建了一个 Character child class,其中包含一个 Weapon 类型变量和继承自 Character class 的构造函数,该构造函数为角色实例设置名称并创建一个新的 Weapon 实例。 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.当我在另一个脚本文件中使用基本构造函数创建新的 Paladin 类型 object 时,Unity 打印出 CS0103 错误,告诉我我为武器类型变量指定的名称在当前上下文中不存在。

I hit a huge solid brick wall.我撞到了一堵巨大的实心砖墙。 Please help.请帮忙。 I tried everything I can think of.我尝试了我能想到的一切。

Character class:字符 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): Paladin class(八字子):

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();
    }
}

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM