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:
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.
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.
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
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;
}
}
I couldn’t quite decipher what you said, but I can tell you this …
Make sure you are setting the right bool lol
And make sure it’s not being unset, unless it’s inside the statement that’s checking for it.
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?
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