hey my unity gives me an error message it says stack overflow extention after I put my controller script
on my main camera here are my two scripts
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerStats))]
public class Controller : MonoBehaviour {
public PlayerStats _jake;
void Start(){
_jake.level = 3;
_jake.STR = 50;
_jake.DEX = 55;
_jake.DEF = 50;
_jake.vital = 100;
_jake.Magic = 60;
_jake.luck = 45;
_jake.Speed = 56;
_jake.Agility = 46;
_jake.exp.Cur = 0;
}
}
[System.Serializable]
public class PlayerStats : Controller {
public int level;
public string name;
public Stat Hp = new Stat();
public Stat Mp = new Stat();
public int STR;
public int DEX;
public int DEF;
public int vital;
public int Magic;
public int luck;
public int Speed;
public int Agility;
public int melee;
public int ranged;
public int MagA;
public int MagD;
public float active;
public Stat exp = new Stat();
public void addexp(int ex){
exp.Cur += ex;
while(exp.Cur >= exp.Max level < 99){
exp.Cur -= exp.Max;
exp.Max = level *(level+1)* 500;
level++;
upstats();
}
}
void upstats(){
STR++;
DEX++;
DEF++;
vital++;
Magic++;
luck++;
Speed++;
Agility++;
melee =(level*7)+STR;
ranged =(level*7)+DEX;
DEF =(level*2)+ Agility;
MagA =(level*7)+ Magic;
MagD =(level*2)+ luck;
active =20+((Speed * level)/1500);
Hp.Max =((level * 5)+ vital)*10;
Hp.Cur = Hp.Max;
Mp.Max =((level * 5)+ Magic)*10;
Mp.Cur = Mp.Max;
}
public class Stat{
public int Cur;
public int Max;
}
public PlayerStats(int level,string name,int STR,int DEX,int DEF,int vital,int Magic,int luck,int speed,int Agility,int exp){
this.level = 1;
this.name = name;
this.STR = STR;
this.DEF = DEF;
this.vital = vital;
this.Magic = Magic;
this.luck = luck;
this.Speed = speed;
this.Agility = Agility;
addexp(exp);
}
}
Did you add the dependancies to the 2nd script (if they are in fact 2 seperate files)?
using UnityEngine;
using System.Collections;
Also, you might want to declare the PlayerStats class inside the Controller class, and rename the Controller class to something specific, like Entity_Controller or something, might cause a conflict with other declarations.
Stack overflow errors always remind me of arrays that are out of bounds or variables that have too high a value. Check the values of your variables by putting in a lot of Debug.LogWarning() messages to locate the problem
EDIT: …and keep an eye on your console when trying to run inside the Unity editor.
I found that sometimes it helps to just clear the console and try to run the game again. I have had a lot of errors that showed because I was editing the script, then switched to the editor. It isn’t always as fast with updating and checking the script.
Your addexp method has a while loop that is probably infinite. Also, since PlayerStats derives from MonoBehaviour there is no point in defining a constructor for it.
It doesn’t have to derive from MonoBehaviour if Controller does, but you will have to make PlayerStats a subclass of Controller. It really depends on how you want to use that class; Do you want AI to be using the Controller class as well?
You can make a subclass by simply copying the PlayerStats class into your Controller class. I usually start a class with subclasses before adding properties, methods, triggers, etc.
At any rate, was it solved already? And the AddExp method looks sound, don’t see a problem there, as long as exp.Cur is decreased or level is increased inside the loop.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerStats))]
public class Controller : MonoBehaviour {
public PlayerStats _jake;
void Start(){
_jake.level = 3;
_jake.STR = 50;
_jake.DEX = 55;
_jake.DEF = 50;
_jake.vital = 100;
_jake.Magic = 60;
_jake.luck = 45;
_jake.Speed = 56;
_jake.Agility = 46;
_jake.exp.Cur = 0;
}
}
using UnityEngine;
using System.Collections;
[System.Serializable]
public class PlayerStats : Controller {
public int level;
public string name;
public Stat Hp = new Stat();
public Stat Mp = new Stat();
public int STR;
public int DEX;
public int DEF;
public int vital;
public int Magic;
public int luck;
public int Speed;
public int Agility;
public int melee;
public int ranged;
public int MagA;
public int MagD;
public float active;
public Stat exp = new Stat();
public void addexp(int ex){
exp.Cur += ex;
while(exp.Cur >= exp.Max level < 99){
exp.Cur -= exp.Max;
exp.Max = level *(level+1)* 500;
level++;
upstats();
}
}
void upstats(){
STR++;
DEX++;
DEF++;
vital++;
Magic++;
luck++;
Speed++;
Agility++;
melee =(level*7)+STR;
ranged =(level*7)+DEX;
DEF =(level*2)+ Agility;
MagA =(level*7)+ Magic;
MagD =(level*2)+ luck;
active =20+((Speed * level)/1500);
Hp.Max =((level * 5)+ vital)*10;
Hp.Cur = Hp.Max;
Mp.Max =((level * 5)+ Magic)*10;
Mp.Cur = Mp.Max;
}
public class Stat{
public int Cur;
public int Max;
}
public PlayerStats(int level,string name,int STR,int DEX,int DEF,int vital,int Magic,int luck,int speed,int Agility,int exp){
this.level = 1;
this.name = name;
this.STR = STR;
this.DEF = DEF;
this.vital = vital;
this.Magic = Magic;
this.luck = luck;
this.Speed = speed;
this.Agility = Agility;
addexp(exp);
}
}
Just take off the inheritance in general… You can then use the script as you originally intended… That white loop looks a bit off though.
On start up make a new instance of that class.
[[System.Serializable]
public class MyNewClass()
{
public string myName;
public float myValue;
}
public class MyAttachedScript: Monodevelopment
{
public MyNewClass anything = new MyNewClass();
public List<MyNewClass> anythings = new List<MyNewClass>();
public Start()
{
anything.myName = "Some Item";
anything.myValue = 10;
anythings.Add(anything);
}
}
I went in and tested it for you. Just throw this all into Controller.cs
I also notice that you missed this.DEX = DEX;
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerStats))]
public class Controller : MonoBehaviour
{
public PlayerStats _jake;
void Start()
{
_jake = new PlayerStats(3, "Jake", 50, 55, 50, 100, 60, 45, 56, 46, 0);
_jake.SetUp();
}
void AddExp(int amount)
{
_jake.addexp(amount);
}
}
public class Stat
{
public int Cur;
public int Max;
}
[System.Serializable]
public class PlayerStats
{
public int level;
public string name;
public Stat Hp = new Stat();
public Stat Mp = new Stat();
public int STR;
public int DEX;
public int DEF;
public int vital;
public int Magic;
public int luck;
public int Speed;
public int Agility;
public int melee;
public int ranged;
public int MagA;
public int MagD;
public float active;
public Stat exp = new Stat();
public void SetUp()
{
exp = new Stat();
Hp = new Stat();
Mp = new Stat();
}
public void addexp(int ex)
{
exp.Cur += ex;
if(exp.Cur >= exp.Max level < 99)
{
exp.Cur -= exp.Max;
exp.Max = level *(level+1)* 500;
level++;
upstats();
}
}
void upstats()
{
STR++;
DEX++;
DEF++;
vital++;
Magic++;
luck++;
Speed++;
Agility++;
melee =(level*7)+STR;
ranged =(level*7)+DEX;
DEF =(level*2)+ Agility;
MagA =(level*7)+ Magic;
MagD =(level*2)+ luck;
active =20+((Speed * level)/1500);
Hp.Max =((level * 5)+ vital)*10;
Hp.Cur = Hp.Max;
Mp.Max =((level * 5)+ Magic)*10;
Mp.Cur = Mp.Max;
}
public PlayerStats(int level, string name, int STR, int DEX, int DEF, int vital, int Magic, int luck, int speed, int Agility, int exp)
{
this.level = 1;
this.name = name;
this.STR = STR;
this.DEF = DEF;
this.vital = vital;
this.Magic = Magic;
this.luck = luck;
this.Speed = speed;
this.Agility = Agility;
this.DEX = DEX;
addexp(exp);
}
}
so can I delete my PlayerStats script now that PlayerStats is in my controller script and can I also add more party member characters too?
and is this new script able to add enemies too or do I need to write a new script for that?
and thanks for checking it did not even know that I forgot to add this.DEX = DEX; thanks for telling me