Script causes crash

Hello,

I’m making 2D RPG game. I made script that is used for Levels, Health, Mana, Strenght etc.

public class mainClass {

    private string _Imie;
    public int _LVL;
    public int _HP;
    public int _MP;

    public mainClass(){
        _Imie = string.Empty;
        _LVL = 1;
        _HP = 100;
        _MP = 100;
        }

    public string Imie
    {
        get{ return _Imie; }
        set{ _Imie = value; }
    }

    public int Level
    {
        get{ return _LVL; }
        set{ _LVL = value; }
    }

    public int Health
    {
        get{ return _HP; }
        set{ _HP = value; }
    }

    public int Mana
    {
        get{ return _MP; }
        set{ _MP = value; }
    }

}

And when I want to use it while fighting scene is played, Unity crashes.
Here is script that cause crash.

using UnityEngine;
using System.Collections;

public class GUIfight : MonoBehaviour {

    private mainClass _Hero = new mainClass();

    public GUISkin gSkin;

    void OnGUI()
    {
        GUI.skin = gSkin;
        GUI.Box(new Rect(Screen.width/2 - 500, Screen.height/2 + 200, 100, 100), "\n\n\n\n\t");
        GUILayout.BeginArea(new Rect(Screen.width/2 - 500, Screen.height/2 + 200, 100, 100));

        GUILayout.Label(" Level " + _Hero.Level);

        GUILayout.Label(" HP " + _Hero.Health);

        GUILayout.Label(" MP " + _Hero.Mana);

        GUILayout.EndArea();
    }
}

If I remove private mainClass _Hero = new mainClass(); then everything works fine.

Crash as in freezes up and must be force quit? Or crash as in exits unity and shows the big report screen?

If it’s a freeze then the problem is an infinite loop. Try commenting out the lines in your constructor one at a time. I suspect you have a self referencing property, but I can’t see it.

Your set up is a bit odd. Typically you would not make your backing variables public, or access them directly.

I’m getting message “Unity Editor has stopped working”.

Refresh