我有一个涉及2个不同脚本的问题。我试图实现的是使用字符串来访问像标题所示的静态变量。对静态变量使用字符串?
我的第一个剧本:
using UnityEngine;
using System.Collections;
public class GameInformation : MonoBehaviour
{
//Upgrades Info, 1 is bought while 0 is not bought
public static int TipJar;
}
我的第二个脚本:
using UnityEngine;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class Upgrades : MonoBehaviour
{
public List<PlayerTank> playerList;
public class PlayerTank // Here is my class for PlayerTank
{
public string Name;
public string Value;
// This is a constructor to make an instance of my class.
public PlayerTank (string NewName, int NewValue)
{
Name = NewName;
Value = NewValue;
}
}
void Start()
{
playerList = new List<PlayerTank>();
playerList.Add (new PlayerTank("TipJar", GameInformation.TipJar));
//To loop all the list
for(int i = 0; i < playerList.Count; i++)
{
int TempInt = i;
playerList[i].NewValue += 1; //This line works but Gameinformation.TipJar will still contain the value 0, i want it to be 1.
}
}
}
我的目标是更新GameInformation.TipJar
值,但它一直含0
。
我试图用GameInformation.playerList[i].Name(which is TipJar) += 1
替换playerList[i].NewValue += 1;
。
我一直在寻找一段时间,我找不到解决方案,任何想法?
这个问题不是很清楚,你想达到什么目的?你有预期的产量吗? – Bassie
对不起,你想达到什么目的?很不清楚 – Tinwor
你的GameInformation类只定义了一个int var“TipJar” – joreldraw