Need Help subtracting 1 from float animator condition

I’ve got gardens that grow tomatoes.
Tomatoes are ammo too.
When tomatoes are harvested, they are stored in the garden animator as a float condition.
My throwTomato script tells my harvestTomatowL script that a tomato has been thrown. (tomatoThrown)
I then want my harvestTomatowL to see that tomatoThrown == true

*This is where I’m stuck:

rawtLvl = rawtLvl - 1f <----i want to subtract one tomato when tomatoThrown == true
then tomatoThrown = false;

*check bottom of code

    void Update ()
    {
        newtLvl = animtomatoCount.GetFloat ("tomatoAmount") + 4f;//newtomatolevel(newtLvl) Gets tomatoAmount(in tomatoCount) + 4
        rawtLvl = animtomatoCount.GetFloat ("tomatoAmount");
        animtomatoCount.SetFloat ("tomatoAmount", rawtLvl);


        if (canPlant == true && Input.GetKeyDown (KeyCode.H) && canHarvest == true)//is player close enough and pushing 'H' and canharvest == true
        {
            animGarden.SetFloat ("tomatowaterLevel", 25f);//Sets tomato plant water to 25
            animtomatoCount.SetFloat ("tomatoAmount", newtLvl);//Sets newtomatoLevel to tomatoAmount
        }

        if (rawtLvl > 48) //player holds max 48 tomatos
        {
            animtomatoCount.SetFloat ("tomatoAmount", 48f);
        }
         
        if (rawtLvl > 0f) //if tomatoAmount more than 1, Player has ammo
        {
            Player.GetComponent<throwTomato> ().hastomatoAmmo = true; // tomato has been thrown
        }



        if (tomatoThrown == true) //if they throw the tomato, subtract one tomato
        {
           /*
*This is where I'm stuck:

rawtLvl = rawtLvl - 1f <----i want to subtract one tomato when tomatoThrown == true
then tomatoThrown = false;
*/


            tomatoThrown = false; // tomato is no-longer thrown
        }
    }

i’ve been playing with for loops but that feels overboard. this seems simple but doesn’t work:

-thanks for any help

I don’t understand the issue. Why can’t you subtract 1? Granted, having all this code in Update is probably not the best place for it…but that’s another thing.

my current issue is it is not setting tomatoThrown = true. Here is the code for throwing:

void Update ()
    {

        //                 |
        //TOMATO-THROWING \/    (UP, DOWN, LEFT, RIGHT)

        if (Input.GetKeyDown (KeyCode.A) && lastkeyUp2 == true && hastomatoAmmo == true) //up
        {
            Debug.Log ("monkey");
            Garden1.GetComponent<harvestTomatowL> ().tomatoThrown = true;//tomato has been thrown [used by SubtractTomato]
            GameObject tomatoAmmo = (GameObject)Instantiate (tomatoPrefab, transform.position, Quaternion.identity);
            Tomatoes.Add (tomatoAmmo);
            thrownUp = true;//up
            thrownDown = false;
            thrownLeft = false;
            thrownRight = false;
        }

        if (Input.GetKeyDown (KeyCode.A) && lastkeyDown2 == true && hastomatoAmmo == true) //down
        {
            Garden1.GetComponent<harvestTomatowL> ().tomatoThrown = true;//tomato has been thrown [used by SubtractTomato]
            GameObject tomatoAmmo = (GameObject)Instantiate (tomatoPrefab, transform.position, Quaternion.identity);
            Tomatoes.Add (tomatoAmmo);
            thrownDown = true;//down
            thrownUp = false;
            thrownLeft = false;
            thrownRight = false;
        }

        if (Input.GetKeyDown (KeyCode.A) && lastkeyLeft2 == true && hastomatoAmmo == true) //left
        {
            Garden1.GetComponent<harvestTomatowL> ().tomatoThrown = true;//tomato has been thrown [used by SubtractTomato]
            GameObject tomatoAmmo = (GameObject)Instantiate (tomatoPrefab, transform.position, Quaternion.identity);
            Tomatoes.Add (tomatoAmmo);
            thrownLeft = true;//left
            thrownUp = false;
            thrownDown = false;
            thrownRight = false;
        }

        if (Input.GetKeyDown (KeyCode.A) && lastkeyRight2 == true && hastomatoAmmo == true) //right
        {
            Garden1.GetComponent<harvestTomatowL> ().tomatoThrown = true;//tomato has been thrown [used by SubtractTomato]
            GameObject tomatoAmmo = (GameObject)Instantiate (tomatoPrefab, transform.position, Quaternion.identity);
            Tomatoes.Add (tomatoAmmo);
            thrownRight = true;//right
            thrownLeft = false;
            thrownUp = false;
            thrownDown = false;
        }

        for (int i = 0; i < Tomatoes.Count; i++) // Tomato(sprite) movement up
        {
            GameObject goTomato = Tomatoes[i];
            if (goTomato != null && thrownUp == true)//up
            {
                goTomato.transform.Translate(new Vector3(0,1,0) * Time.deltaTime * tomatoVelocity);
            }

            if (goTomato != null && thrownDown == true)//down
            {
                goTomato.transform.Translate(new Vector3(0,-1,0) * Time.deltaTime * tomatoVelocity);
            }

            if (goTomato != null && thrownLeft == true)//left
            {
                goTomato.transform.Translate(new Vector3(-1,0,0) * Time.deltaTime * tomatoVelocity);
            }

            if (goTomato != null && thrownRight == true)//right
            {
                goTomato.transform.Translate(new Vector3(1,0,0) * Time.deltaTime * tomatoVelocity);
            }
        }


    }

baiscally its turning tomatoThrown = true when you push A and turning tomatoThrown = false
when the tomato is subtracted from the animator. except its not registering when I push A…

If your throw is triggering properly, (you hit a key and your tomato flies off) then you’ll need to make sure Garden1 is targeting what it’s suppose to. Otherwise, add more Debug.Log to your code to make sure it’s not hitting where it’s suppose to. You have your subtract commented out right now, but you should be able to remove the comment and just test what is going on. What you’ve shown doesn’t appear wrong.

1 Like

You could shorten so much of that. GetKeyDown ‘A’ and hasAmmo for starters. Each condition you have has those 2 parts in common.

Back to your question. I believe you need to set the animator float after you modify it. There’s nothing, not even in commented code, that was doing that. That means the next time your loop runs, it reads back the old value.
Plus you could reduce 1 call to GetFloat at the top of your update loop, by getting it once and using it twice up there :wink:

Hope that helps :slight_smile:

1 Like

I’d highly recommend trying to think about how you can put this code outside of Update().

For example:

  • add tomato → has ammo (given)
  • get tomato → clamp to max 48
  • can throw (has ammo?) → yes … reduce 1, set float (the end).
  • throwing… only happens when you throw, not every update loop
    … etc :slight_smile:

Just trying to be helpful. Easier (/better) to catch it earlier on! :slight_smile:

1 Like

i’m going to spend some time and clean this up. itll be much easier to see whats going on. mucho appreciato por favor

    void Update ()
    {

        //                 |
        //TOMATO-THROWING \/    (UP, DOWN, LEFT, RIGHT)

        if (Input.GetKeyDown (KeyCode.A) && hastomatoAmmo == true) //up
        {
            Garden1.GetComponent<harvestTomatowL> ().tomatoThrown = true;//tomato has been thrown [used by SubtractTomato]
            GameObject tomatoAmmo = (GameObject)Instantiate (tomatoPrefab, transform.position, Quaternion.identity);
            Tomatoes.Add (tomatoAmmo);

            if (lastkeyUp2 == true)
            {
                thrownUp = true;//up
                thrownDown = false;
                thrownLeft = false;
                thrownRight = false;
            }

            if (lastkeyDown2 == true)
            {
                thrownUp = false;
                thrownDown = true;//down
                thrownLeft = false;
                thrownRight = false;
            }

            if (lastkeyLeft2 == true)
            {
                thrownUp = false;
                thrownDown = false;
                thrownLeft = true;//left
                thrownRight = false;
            }

            if (lastkeyRight2 == true)
            {
                thrownUp = false;
                thrownDown = false;
                thrownLeft = false;
                thrownRight = true;//right
            }
        }

        for (int i = 0; i < Tomatoes.Count; i++) // Tomato(sprite) movement
        {
            GameObject goTomato = Tomatoes[i];

            if (goTomato != null)
            {
                if (thrownUp == true)
                {
                    goTomato.transform.Translate (new Vector3 (0, 1, 0) * Time.deltaTime * tomatoVelocity);
                }
                if (thrownDown == true)
                {
                    goTomato.transform.Translate(new Vector3(0,-1,0) * Time.deltaTime * tomatoVelocity);
                }
                if (thrownLeft == true)
                {
                    goTomato.transform.Translate(new Vector3(-1,0,0) * Time.deltaTime * tomatoVelocity);
                }
                if (thrownRight == true)
                {
                    goTomato.transform.Translate(new Vector3(1,0,0) * Time.deltaTime * tomatoVelocity);
                }
            }
               
        }

Cool. Hope you didn’t miss the part about setting the animator float after you subtract it (which was my thought for an answer for ya) :slight_smile:

1 Like

so this has been my script. what you said. i found i missed a transition in the animator -.-

void Update () 
    {        
    adjrawtLvl = rawtLvl - 1f;
        rawtLvl = animtomatoCount.GetFloat ("tomatoAmount");


if (tomatoThrown == true) //if they throw the tomato, subtract one tomato
        {

            animtomatoCount.SetFloat ("tomatoAmount", adjrawtLvl);//Sets newtomatoLevel to tomatoAmount

            tomatoThrown = false; // tomato is no-longer thrown
        }

Cool. I think you want to adjust the tomatoAmount inside the ‘Thrown’ statement, but other than that, it’s good :wink: heh

1 Like

Like this? :slight_smile:

void Update() {
      // now, to be honest, you only need to fetch this value once in Start() or something, and just update the 
     // variable as you go, but for simplicity, I left it here.. :)
    rawtLvl = animtomatoCount.GetFloat ("tomatoAmount");
if (tomatoThrown) {
     rawtLvl--;
     animtomatoCount.SetFloat("tomatoAmount", rawtLvl);
     tomatoThrown = false;
    }
 }
1 Like

so it would work if the tomatoThrown would turn true…gahaha.

here is where it is turning the tomatoThrown true:

if (Input.GetKeyDown (KeyCode.A)) //up
        {
            AmmoCheck ();
            if(hastomatoAmmo == true)
            {
            Garden1.GetComponent<harvestTomatowL> ().tomatoThrown = true;//tomato has been thrown [used by SubtractTomato]

which then responds here:

            if (tomatoThrown == true)
            {
                rawtLvl = animtomatoCount.GetFloat ("tomatoAmount");//Sets newtomatoLevel to tomatoAmount
                rawtLvl--;
                animtomatoCount.SetFloat ("tomatoAmount", rawtLvl);
                tomatoThrown = false; // tomato is no-longer thrown
            }

if i click the tomatoThrown bool manually, it counts it down. so it works… but whats wrong with :

Garden1.GetComponent<harvestTomatowL> ().tomatoThrown = true;

its because im on two different gameobjects i think. Player and Garden.

Player(GameObject) uses throwTomatos(script) to throw tomato while Garden(GameObject) uses harvestTomatowL(script) to subtract one tomato

I couldn’t quite decipher what you said, but I can tell you this …
Make sure you are setting the right bool :slight_smile: lol
And make sure it’s not being unset, unless it’s inside the statement that’s checking for it.

1 Like

so i made a new tester bool Tomatotest.

this bool is in script 2
this bool is changed in script 1

script 1:

        if (Input.GetKeyDown (KeyCode.Y)) //up
        {
            Garden1.GetComponent<harvestTomatowL> ().tomatoTest = true;//tomato has been thrown [used by SubtractTomato]
        }

^it is not changing the bool in script 2 for some reason.

the only thing i can see standing in the way is that player(holding script 1) is DontDestroyOnLoad.

when a GameObject is DontDestroyOnLoad, is it loaded on its own static scene or something? from the heirarchy it looks like thats the case… if so, how do i reference it?

I think you and I had a chat (part of a thread) about how to keep contact with a script if you change scenes (to keep the reference). You can use something like that, if that’s what you mean.

If you lost the reference, though, it should be giving you a null ref. error?

1 Like

yes we did. i never ended up getting Garden1 to not be destroyed. DoNotDestroyOnLoad works but i cant figure out what is keeping Garden1 from being added to it

OMG i got it!

DontDestroyOnLoad (GameObject.Find(“garden1”));

did the trick. had to Find it for some reason? its a root object so thats weird…?

Glad you got it. Not sure what was wrong before. DontDestroyOnLoad can be called on any root game object :slight_smile:

1 Like