1

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 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: enter image description here

Code:

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;
        }
    }
}

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Joe, I initially thought I could do it this way Thanks for all of your help!
what happens if it is just a string variable ? no dragging
hi Rifat. don't know what you mean. just try public string x and see what happens. you can type in a string in the Editor, in Inspector in the slot. Good luck
0

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>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.