父级到子级静态变量保持不变

时间:2019-02-17 11:56:18

标签: c# unity3d

第一个随机数保持不变,例如其50的子类文本将为50,而另一个随机数例如为45,则应为50 + 45,而不用45代替该值

家长班

    public class BattleSystemScript : GameManagerRevamped
   {
      static public int CoinsRandom;
      public TextMeshProUGUI Coins;

         void Start()
         {  
           CoinsRandom = Random.Range(30, 50);
           Coins.text = CoinsRandom.ToString();
         } 
  }

儿童班

public class PlayerValues : BattleSystemScript
{
  int CoinsRandomValue;
  public TextMeshProUGUI PlayerCoins;         
       void Start()
       {
        CoinsRandomValue += CoinsRandom;
        PlayerCoins.text = CoinsRandomValue.ToString();
       }
}

2 个答案:

答案 0 :(得分:1)

使父virtualpublic class BattleSystemScript : GameManagerRevamped { static public int CoinsRandom; public TextMeshProUGUI Coins; protected virtual void Start() { CoinsRandom = Random.Range(30, 50); Coins.text = CoinsRandom.ToString(); } } 方法

override

然后在子类中public class PlayerValues : BattleSystemScript { int CoinsRandomValue; public TextMeshProUGUI PlayerCoins; protected override void Start() { // call to the parent Start base.Start(); CoinsRandomValue += CoinsRandom; PlayerCoins.text = CoinsRandomValue.ToString(); } } 对其进行扩展

for-loop

答案 1 :(得分:0)

所以我只需要让子类静态化大声笑

--->

static public int CoinsRandomValue;
static public int DiamondsValues;
static public int ExperienceRandomValue;