2016-09-12 3 views
0

私は2つの異なるスクリプトを含む問題があります。私が達成しようとしているのは、タイトルのように静的変数にアクセスするためにStringを使用することです。静的変数で文字列を使用しますか?

私の最初のスクリプト:

using UnityEngine; 
using System.Collections; 

public class GameInformation : MonoBehaviour 
{ 
    //Upgrades Info, 1 is bought while 0 is not bought 
    public static int TipJar; 
} 

は私の2番目のスクリプト:

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) += 1playerList[i].NewValue += 1;を交換しようとしています。

私は今、いくつかの時間を探していると私は解決策、任意のアイデアを見つけることができませんでしたか?

+1

この質問はあまり明確ではありませんが、達成しようとしているのは何ですか?あなたは期待される成果を得ていますか? – Bassie

+0

申し訳ありませんが、何を達成しようとしていますか?非常に不明です – Tinwor

+0

あなたのGameInformationクラスはint var "TipJar"しか定義していません – joreldraw

答えて

0

ような何かをしたいと思いますが、私の目標は、時に内部ループのための(1)になるようにGameInformation.TipJarの値を作ることだけです。あなたからのこのコメントを撮る

、私はGameInformation.TipJarが(それは関係なく、playerListの内容の、1時にあなたのループであるべきという意味)は、グローバルであることを意味していることを前提としています。その後、ちょうど前GameInformation.TipJar = 1;のような静的に設定し、それが正しい場合、静的変数(簡単に)

を使用

1:私はあなたにこの仮定の下で、あなたの問題を解決するための2つの方法を提供しますループが開始し、ループの直後にGameInformation.TipJar = 0;に設定されます。 gio.GetType().GetField("TipJar").SetValue(gio, value):あなたはGameInformationオブジェクトへの参照を(それがgio命名されたと言う)がある場合は

2.反射

は、あなたが持つ文字列としてフィールド名でそのフィールドを設定することができます。 (参照:https://msdn.microsoft.com/en-us/library/6z33zd7h(v=vs.110).aspx)それはあなたの質問に答えていない場合は


、その後、私は申し訳ありませんが、私は本当にあなたがやろうとしているものを得ることはありません。

+0

ああありがとうalot :) – OddTuna

1

ほとんどのタイプミス?

playerList[i].Name += 1; 

変更

GameInformation.playerList[i].Name += 1; //This line will not work 

またNameをフィールドタイプStringであるので、私は1がここで理にかなっていることを疑います。私はあなたが不明で質問して申し訳ありません

playerList[i].Name += (i + 1); 
関連する問題