How to use the same script on multiple game objects in unity?

后端 未结 2 890
情深已故
情深已故 2021-01-24 01:56

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

When the FillAmount = 1 I want to reset that specific Image to zero. I want to be able to use t

相关标签:
2条回答
  • 2021-01-24 02:56

    Joe's answer is good, but wanted to add another option to answer the titular question: Since your class derives from Monobehavior, you can add your script to ANY game object at Run-time with the following code-line.

    someobject.gameObject.AddComponent<CoinFill>();
    
    0 讨论(0)
  • 2021-01-24 03:01

    Say you have THREE different pennies, A B and C

    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) look at A. drag your script ON TO A.

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

    4) drag A from the hierarchy ON TO THOSE INSPECTOR VARIABLE SLOTS, on 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) now forget A. look only at B

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

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

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

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

    Run. notice you see three of those.

    You now have three INDEPENDENT objects with INDEPENDENT scripts! Enjoy

    0 讨论(0)
提交回复
热议问题