Home>

When I created a Fire class that inherits BaseMagic in Unity, I got 4 errors, but I don't understand the cause. I would like to know what is wrong. Thank you.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BattleScene;
using BattleScene.Chara;
namespace BattleScene.Magic {
public class BaseMagic: IMagic
{
    public int DownMP {get;set;}
    TextManager textmanager = GameObject.Find ("battletext"). GetComponent<TextManager>();
    public void Use (Player attacker, Player defender);
    public bool canUse (Player player) {
       if (player.MP>= DownMP) {
           return true;
       }
       return false;
    }
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BattleScene;
using BattleScene.Chara;
namespace BattleScene.Magic {
public class Fire: BaseMagic
{
    public Fire () {
        public int maxDamage {get;set;} = 30;
        public int minDamage {get;set;} = 10
        base.DownMP = 20;
    }
    public override void Use (Player attacker, Player defender) {
        int calcedDamage = UnityEngine.Random.Range (this.minDamage, this.maxDamage + 1);
        base.textmanager.battleLog ($"Fire of {attacker.PlayerName}! {CalcedDamage} damage to {defender.PlayerName}");
        attacker.MP = attacker.MP-base.DownMP;// mp consumption -20
        defender.damage (calcedDamage);
        attacker.AttackFinished = true;
    }
}
}
Assets \ MainBattle \ BattleScene \ Magic \ Fire.cs (11,19): error CS1513:} expected
Assets \ MainBattle \ BattleScene \ Magic \ Fire.cs (14,9): error CS1519: Invalid token'base' in class, struct, or interface member declaration
Assets \ MainBattle \ BattleScene \ Magic \ Fire.cs (14,21): error CS1519: Invalid token'=' in class, struct, or interface member declaration
Assets \ MainBattle \ BattleScene \ Magic \ Fire.cs (26,1): error CS1022: Type or namespace definition, or end-of-file expected
  • Answer # 1

      public Fire () {
            public int maxDamage {get;set;} = 30;
            public int minDamage {get;set;} = 10
            base.DownMP = 20;
        }

    Since "what is defined in the function can be used only in the function", maxDamage and minDamage can be used only in Fire (), and public is added to mean "make it accessible anywhere". So it's no longer meaningful to the compiler.
    If you want to refer to maxDamage and minDamage from other classes, you should define them outside of Fire ().

  • Answer # 2

    The line of minDamage and the semicolon are missing.
    Also, I'm using public in the constructor, but I think it's strange.
    Find out what you want to do on the line that says public and what is the correct way to write it.