修改另一个类的变量c#

时间:2014-12-24 05:34:35

标签: c# unity3d

我必须调用变量" rotSpeed"在我的情况下。为了班级困难,并相应地改变它。我怎么称呼它?

变量在另一个类中。

public float rotSpeed = 90f;

这是变量

我需要将其称为此代码:

public class Difficulty : MonoBehaviour {

public float multiplyerx1 = .25f;
public float multiplyery1 = .4f;
public float multiplyerx2 = .25f;
public float multiplyery2 = .4f;
public Texture BackgroundTexture;


void OnGUI(){

    GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), BackgroundTexture);
    //Hard
    if (GUI.Button( new Rect(Screen.width * multiplyerx1, Screen.height * multiplyery1, Screen.width * .5f, Screen.height * .1f), "Hard")){
        //Put in change of rotspeed
        Application.LoadLevel("GameScene");
    }
    //Easy
    if (GUI.Button( new Rect(Screen.width * multiplyerx2, Screen.height * multiplyery2, Screen.width * .5f, Screen.height * .1f), "Easy")){
        //Put in change of rotspeed
        Application.LoadLevel("GameScene");
    }

}

1 个答案:

答案 0 :(得分:0)

将此字段设为静态,如下所示: -

public static float rotSpeed = 90f;

并且具有此字段的类应该是静态的但不是必需的,您可以如下所示获取: -

ClassName.rotSpeed;

如果您的字段将来可能会有所不同,请使用如下所示的属性: -

 private static float rotSpeed;
 public static float RotSpeed
   { 
        get { return rotSpeed; }
        set { rotSpeed = value; }

   }

并按以下方式致电: -

 ClassName.RotSpeed;