如何在多个游戏对象上统一使用同一脚本? [英] How to use the same script on multiple game objects in unity?

查看:543
本文介绍了如何在多个游戏对象上统一使用同一脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本CoinFill,它可以显示径向进度条.

I have a script CoinFill which makes a radial progress bar.

当FillAmount = 1时,我想将该特定图像重置为零.我希望能够将其用于多个GameObject.问题在于,当第一个FillAmount = 1时,速度更快的便士,您可以单击小节,其填充可能为50%,然后便士将重置为0.但是,如果小节为1它不会重置自己,只有一分钱才能休息.

When the FillAmount = 1 I want to reset that specific Image to zero. I want to be able to use this for multiple GameObjects. The problem is that when the first FillAmount=1, the penny which speed is faster, you can click on the nickle, which may be at 50% fill and then the penny will reset to 0. However if the nickle is at 1 it will not reset itself, only the penny will rest.

我要执行的操作的图像:

Image of what I am trying to do:

代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class CoinFill : MonoBehaviour {

    public SavingsAccountManager sam;

    public float fillCoinSpeed;
    public Image coinFill;
    public float maxCoinFill = 100f;
    public float minCoinFill = 0f;
    public float currentCoinFill;


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


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

        coinFill.fillAmount = currentCoinFill / maxCoinFill;
    }
 //Penny Button
    public void PennyPush()
    {
        if (coinFill.fillAmount == 1)
        {
           sam.savingsAccountAmount += .01f;
           sam.savingsAccountText.text = sam.savingsAccountAmount.ToString("f2");
           currentCoinFill = minCoinFill;
        }

    }

 //Nickle Button
    public void NicklePush()
    {
        if (coinFill.fillAmount == 1)
        {
            sam.savingsAccountAmount += .05f;
            sam.savingsAccountText.text = sam.savingsAccountAmount.ToString("f2");
            currentCoinFill = minCoinFill;
        }
    }
}

我不确定是否需要与Penny或Nickle的父母做某事,还是应该使用this或设置一些父母的东西

I am not sure if I need to do something with a parent of the Penny or Nickle or if I should be using a this or set up some parent thing

推荐答案

假设您有3个便士,A B和C

Say you have THREE different pennies, A B and C

1)确实在场景中创建了3个便士..即,制作了三个新游戏对象并添加了图形或其他内容.请确保在每个游戏对象上正确设置名称

1) so indeed create the THREE pennies in the scene .. ie, make three new game objects and add the graphics or whatever. be sure to set the name properly on each game object

2)查看A.将脚本拖到A.

3)查看脚本上的检查器变量插槽实际上在A上.

3) look at the inspector variable slots ON THE SCRIPT actually ON A.

4)将层次结构中的A拖到A上的这些检查器变量插槽上

4) drag A from the hierarchy ON TO THOSE INSPECTOR VARIABLE SLOTS, on A

即,完全可以将项目拖到自身上" .换句话说,脚本中的变量将仅引用该项目本身,A"

ie, it's totally ok to drag an item "on to itself". in other words your variables in the script will refer simply to "that item itself, A"

5)现在忘记了A.只看B

5) now forget A. look only at B

6)再次将脚本拖到B上,将B拖到自身上"以填充插槽

6) drag the script on to B. again, drag B "on to itself" to fill the slots

7)现在C.再次将脚本拖到C.上,将C拖到自身上"以填充插槽

7) now C. drag the script on to C. again, drag C "on to itself" to fill the slots

位于脚本顶部(在Awake中)或添加此

at the top of the script (in Awake) or whatever add this

Debug.Log(此特定脚本位于" + gameObject.name)上;

Debug.Log("THIS particular script is on " +gameObject.name);

运行.请注意,您会看到其中的三个.

Run. notice you see three of those.

您现在有了带有INDEPENDENT脚本的三个INDEPENDENT对象!享受

You now have three INDEPENDENT objects with INDEPENDENT scripts! Enjoy

这篇关于如何在多个游戏对象上统一使用同一脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆