2016-08-31 49 views
-1

我有一个类(的上电),其中我有一个布尔马格其公开宣称取一个类的布尔值从另一个类在统一

当我试图从另一个类(结果)访问布尔

     // class Result 

     if(PowerUP.Mag)// need to return the bool value from class PowerUp 
    { 
       // code to run if powerup.mag is true 

     CoinCollectedCounter ++; 
     CoinCounterText.text = ""+CoinCollectedCounter; 
     col.gameObject.SetActive(false); 
    } 

我没有得到布尔value..actually返回null

回答

1

当你的脚本被附加到游戏对象上的一幕:

GameObject.Find("MyGameObject").GetComponent<PowerUp>().Mag;

当您的脚本未连接时:

PowerUp powerUp; // creating object 
powerUp.Mag; // getting variable 
-2

访问它应该被宣布在课堂上PO静态

的另一个类的布尔werup的布尔马格应被声明为

public static bool Mag; 

所以这个声明之后的代码工作

 // class Result 

    if(PowerUP.Mag)// need to return the bool value from class PowerUp 
    { 
      // code to run if powerup.mag is true 

    CoinCollectedCounter ++; 
    CoinCounterText.text = ""+CoinCollectedCounter; 
    col.gameObject.SetActive(false); 
    } 
+0

静态变量在游戏执行过程中保留一个值。而是通过'GetComponent ()'获取脚本实例,并使用此实例访问该值。 –