Search Unity

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

Discussion in 'Scripting' started by Fireiko, Jan 14, 2020.

  1. Fireiko

    Fireiko

    Joined:
    Jan 12, 2020
    Posts:
    1
    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);
    }
    }
    }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,071
    Use [code ]code tags[/code], 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.
     
  3. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,206
    1. you put this inside private void Start(); you cannot do that, you need to move it where it belongs,
    2. 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:
    Code (CSharp):
    1. public class enemy : MonoBehaviour
    2. {
    3.     [System.Serializable]
    4.     public class EnemyStats
    5.     {
    6.         public int maxHealth = 100;
    7.  
    8.         private int _curHealth;
    9.         public int curHealth
    10.         {
    11.  
    12.             get { return _curHealth; }
    13.             set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }
    14.         }
    15.  
    16.         public void Init()
    17.         {
    18.             curHealth = maxHealth;
    19.         }
    20.     }
    21.  
    22.     public EnemyStats stats = new EnemyStats();
    23.  
    24.     [Header("Optina;: ")]
    25.     [SerializeField]
    26.     private StatusIndicator statusIndicator;
    27.  
    28.     void Start()
    29.     {
    30.         stats.Init();
    31.  
    32.         if(statusIndicator != null)
    33.         {
    34.             statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
    35.         }
    36.  
    37.         public void DamageEnemy(int damage)
    38.         {
    39.             stats.curHealth -= damage;
    40.  
    41.             if(stats.curHealth <= 0)
    42.             {
    43.                 GameMaster.KillEnemy(this);
    44.             }
    45.  
    46.         }
    47.        
    48.         if (statusIndicator != null)
    49.         {
    50.             statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
    51.         }
    52.     }
    53. }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,883
    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)
     
  5. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,206
    Conventions exist for a reason; it's better following them in coding otherwise you end up with a mess. :)
     
    Joe-Censored likes this.
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,883
    While true it's better, it's not a requirement. :) 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.
     
unityunity