我有一個涉及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