如果多个游戏对象使用相同的脚本,如何访问特定游戏对象上的变量?

时间:2016-02-24 18:39:41

标签: unity3d

我正在尝试在填充进度条时填充径向进度,这允许按下按钮,按下按钮它会执行某些操作并重置径向进度条。我希望在不同的图像(径向进度条{Green Circle})和GamesObject(按钮)上多次使用它。

由于这方面的帮助,我目前有多个使用相同脚本的GameObject:how-to-use-the-same-script-on-multiple-game-objects-in-unity

CoinFill脚本

public class CoinFill : MonoBehaviour {

public Image coinFill;  //This is a green circle

public float fillCoinSpeed;
public float maxCoinFill = 100f;
public float minCoinFill = 0f;
public float currentCoinFill;
public float coinValue;


// Use this for initialization
void Start()
{
    currentCoinFill = minCoinFill;
}


void Update()
{
    if (currentCoinFill < maxCoinFill)
    {
        currentCoinFill += fillCoinSpeed * Time.deltaTime;
    }

    coinFill.fillAmount = currentCoinFill / maxCoinFill;
}

重置CoinFill脚本的按钮

public class CoinPush : MonoBehaviour {
 //this is used on a button that looks like a coin and is suppose to reset the green circle Image

 public CoinFill cf;

 OnClick(){
        if (cf.coinFill.fillAmount == maxCoinFill)
        {
        cf.currentCoinFill = cf.minCoinFill;
        }
     }
 }

为了在CoinFill脚本上设置图像,我必须公开它。

为了使OnClick()公开,所以我可以使用它,我制作了一个CoinClickManager空GameObject,但这暴露了一个公共的CoinFill Image变量,这就是脚本崩溃的地方。

enter image description here

这是我想要影响的硬币和绿色圆圈的图片。

enter image description here

我的最终目标是能够更改此特定硬币和圆圈上的变量,以及通过脚本而不是检查员按下按钮时的结果。

这是CoinFillManager:

enter image description here

它让我在这里拖动图像,但是它不能访问硬币上的绿色圆圈。

0 个答案:

没有答案
相关问题