堆栈溢出异常:请求的操作导致堆栈溢出



我在Unity2d中使用c#和接口编写了一些代码。 练级系统.cs被放入空的游戏对象中。

我得到错误:

StackOverflowException: The requested operation caused a stack overflow.
PlayerCap.get_actExp () (at Assets/Scripts/player/PlayerCap.cs:17)
PlayerCap.get<message truncated>

上限级别.cs

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

interface ICapLevel
{
float multiplierA { get; set; }
float multiplierB { get; set; }
float multiplierC { get; set; }
float multiplierD { get; set; }
int lvlCap { get; set; }
List<double> expCap { get; set; }
float actExp { get; set; }
void GenerateExpPerLvl();
float RequiredExp(int level);
}

播放器帽.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCap : ICapLevel
{
public float multiplierA { get { return multiplierA; } set { multiplierA = 280f; } }
public float multiplierB { get { return multiplierB; } set { multiplierB = 100f; } }
public float multiplierC { get { return multiplierC; } set { multiplierA = 1.15f; } }
public float multiplierD { get { return multiplierD; } set { multiplierD = 2.3f; } }
public int lvlCap { get { return lvlCap; } set { lvlCap = 210; } }
public List<double> expCap { get { return expCap; } set { expCap = new List<double>(); } }
public float actExp { get { return actExp; } set { actExp = 0f; } }
public void GenerateExpPerLvl()
{
Debug.Log("implementation successful");
for (int expLevel = 1; expLevel <= lvlCap; expLevel++)
{
expCap.Add(RequiredExp(expLevel - 1));
}
}
public float RequiredExp(int level)
{
double formulaRounded;
var formula = multiplierB * (Mathf.Pow(multiplierC, level - 1)) + multiplierA * (Mathf.Pow(level, multiplierD));
if (formula < 1000)
{
if ((formula % 10) == 0)
{
formulaRounded = formula;
return (float)formulaRounded;
}
else
{
formulaRounded = Math.Round((formula / 1000), 1, MidpointRounding.AwayFromZero);
}
}
else
{
formulaRounded = Math.Round((formula / 1000), 0, MidpointRounding.AwayFromZero);
}
return (float)formulaRounded * 1000;
}
}

调平系统.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelingSystem : MonoBehaviour
{
//interface implementation
private PlayerCap _pCap;
private ICapLevel _ICap;
private List<double> _pExpCap;
private int pLvl;
private float _pActExp;
public void Awake()
{
_pCap = new PlayerCap();
_ICap = _pCap;

}
private void Start()
{
_pActExp = _ICap.actExp;
_ICap.GenerateExpPerLvl();
_pExpCap = _ICap.expCap;
}

public void AddExp(float amount)
{
_pActExp += amount;
StartCoroutine(CapLevelCheck(amount));
}
private IEnumerator CapLevelCheck(float amount)
{
while (true)
{
if (_pActExp >= _pExpCap[pLvl])
{
_pActExp -= (float)_pExpCap[pLvl];
AddLevel(1);
} else
{
Debug.Log("You Get " + amount + " Experience and you have " + pLvl + " Lvl. *Click noice!");
break; //otherwise loop will do in infinite
}
yield return 0;
}
}
public void AddLevel(int amount)
{
pLvl += amount;
}
public int GetActualLevel()
{
return pLvl;
}
}

我尝试了一些事情,例如在方法GenerateExpPerLvl((中摆脱并错误消失,所以我的可疑事情是列表变量,但也许我错了。

感谢您的帮助:)

问题就在这里:

public float actExp { get { return actExp; } set { actExp = 0f; } }
// ^ Problem here & ^ same problem here

您正在自己的吸气器和二传器中调用您的财产。它创造了一个无限循环...(每次你打电话actExp它都会自称(

相反,你应该只写(如果访问器内部没有逻辑(:

public float actExp { get; set; }

最新更新