I NEED HELP WITH ERROR CS026

I’m trying to get my code to work and this error comes up and I don’t know what to do I just learned how to use them yesterday this is the code
and the error code

Assets\FightingControl.cs(13,28): error CS0236: A field initializer cannot reference the non-static field, method, or property ‘FightingControl.Lvl’

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

public class FightingControl : MonoBehaviour
{
// First characters stats
public float Hp = 100f;
public float Def = 10.00f;
public float DefPer = 0;
public float Speed = 0;
public float Lvl = 6;
public float RealLvl = Lvl - 5;
// First Enemy stats
public float Edmg = 20;
// Atks for player 1
public float Atk1Power = 0;
public float Atk1Dmg = 25;
// Other
public int EqualChance = 0;
public int CritChance = 0;
// Start is called before the first frame update
void Start()
{
this is where I think the problem is
Atk1Power = Lvl / 5;
Atk1Power = Mathf.Round(Atk1Power);
Atk1Power = Atk1Dmg * Atk1Power;
}
void EnemyOne()
{
Edmg = 700;
EqualChance = Random.Range(1, 10);
CritChance = Random.Range(0, 9);
DefPer = Def * 1000;
DefPer = DefPer * (float)0.0001f;
Edmg = Edmg / DefPer;
if ((Edmg - 1) > Def)
{
Edmg = Mathf.Round(Edmg);
Hp = Hp - Edmg;
Debug.Log(“HIT!”);
}
else if (EqualChance < 6)
{
if (CritChance < 1)
{
Edmg = Edmg + (Edmg / 2);
Edmg = Edmg / DefPer;
Edmg = Mathf.Round(Edmg);
Hp = Hp - Edmg;
Debug.Log(“CRITICAL HIT!”);
}
else
{
Edmg = Mathf.Round(Edmg);
Hp = Hp - Edmg;
Debug.Log(“HIT!”);
}
}
else
{
Debug.Log(“Miss!”);
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
EnemyOne();
}
}

The error message tells you where the error is - “Assets\FightingControl.cs**(13,28)**:…”.

That means It’s on line 13 at column 28.

  1. public float RealLvl = Lvl - 5;

The error message is telling you that you can’t initialize a variable to be the quantity of another variable. So you’ll need to set it in Awake() or Start().

Please edit your post to use code-tags and not plain text.

Thanks.

You’re just making typing mistakes. Go fix them.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

For actual problems, for future reference, if you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379