How to fix 'public' is not valid for this script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemy : MonoBehaviour
{
[System.Serializable]
public class EnemyStats {
public int maxHealth = 100;

private int _curHealth;
public int curHealth {

get { return _curHealth; }
set { _curHealth = Mathf.Clamp (value, 0, maxHealth); }
}

public void Init() {
curHealth = maxHealth;
}
}

public EnemyStats stats = new EnemyStats();

[Header("Optina;: ")]
[SerializeField]
private StatusIndicator statusIndicator;

void Start () {
stats.Init();

if (statusIndicator != null) {
statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
}

public void DamageEnemy (int damage) { (it says it’s here but I don’t know what is wrong)
stats.curHealth -=damage;
if (stats.curHealth <= 0) {
GameMaster.KillEnemy (this);
}
}
if (statusIndicator != null) {
statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
}
}
}

Use code tags, and copy and paste the actual error code, which includes things like line numbers that will help us go right to where the problem is.

  • you put this inside private void Start(); you cannot do that, you need to move it where it belongs,
  • the name of the class must begin with a capital letter; don’t forget to correct the name of the script as well (Enemy.cs).

Here is your script once formatted correctly; all you have to do is remove from void Start() everything that must not be inside Start:

public class enemy : MonoBehaviour
{
    [System.Serializable]
    public class EnemyStats
    {
        public int maxHealth = 100;

        private int _curHealth;
        public int curHealth
        {

            get { return _curHealth; }
            set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }
        }

        public void Init()
        {
            curHealth = maxHealth;
        }
    }

    public EnemyStats stats = new EnemyStats();

    [Header("Optina;: ")]
    [SerializeField]
    private StatusIndicator statusIndicator;

    void Start()
    {
        stats.Init();

        if(statusIndicator != null)
        {
            statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
        }

        public void DamageEnemy(int damage)
        {
            stats.curHealth -= damage;

            if(stats.curHealth <= 0)
            {
                GameMaster.KillEnemy(this);
            }

        }
       
        if (statusIndicator != null)
        {
            statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
        }
    }
}

While it’s certainly good practice to start with a capital letter on the class name, there is nothing actually preventing a person from having their class name be lowercase, as long as the class and script name are the same. (Though I personally find it annoying when it’s all lowercase. lol)

Conventions exist for a reason; it’s better following them in coding otherwise you end up with a mess. :slight_smile:

1 Like

While true it’s better, it’s not a requirement. :slight_smile: And trust me, I’ve dealt with my share of “programmers” who didn’t follow conventions. Luckily, none of them work on my team anymore at work. lol.