Using my StaminaHandler Script, I’m using a count up timer to a float of 3600.0 and then reset to a float of 0.0.
When the game initializes the script is working perfectly, but for some reason anytime I jump then the Update function wants to ignore the parameters that I’ve set for the counter to actually start/stop.
Here is the part of code with extra variables thrown in so you can see where my issues lie.
Q: What language are you using?
A: I’m using C# for the entire project.
Note: I’d much rather someone not give me the answer. I’d like to learn more without asking it to be done correctly for me. Just need help with an issue, not an answer. Might sound a little contradictory but I’m trying to learn not copy. If that makes sense now.
Part 1: Variables
public int playerStamina
public float regenStaminaTime
public float regenStaminaAmount
Your code is getting cut off. Is part 2 your entire Update?
Post code on the forums with Code tags:
Don’t run FindGameObjectWithTag every update. Grab a reference to it once and reuse it. Also, are you sure pController.isGrounded is getting set back to true when your jump is over?
Using GameObject.Find in a if statement like that is really bad, although findwithtag is much less intensive then find. I recommend using other methods to get the GameObject you need because it is a bad habit that people tend to abuse. However, making a single call and cache the value is acceptable, although I would use other methods even still just to get used to not relying on it.
Secondly, use code tags so we can read your code and just post your entire script instead of trying to edit it.
Re-post your code properly and I/we will look at it. I can’t read it in it’s current format.
Please repost the entire code, it is useless if we can’t see your update, start, and other functions.
Also, your problem may actually lie somewhere else but if you don’t post it all we will never know.
Out of curiosity how would you grab the game object? I planned on having multiple characters with different stat values.
EDIT: I figured another call method instead of using GameObject.FindGameObjectWithTag. Swapped the GameObject to it’s own name. So now I’m just using playerJoe & referencing it to other scripts using pController.playerJoe
Is that what you meant by using another calling method?
For your specific setup i can’t answer that fully since I don’t know how your game and gameObjects are setup. However, the easiest way is to have a public GameObject and drag it into the inspector. Now, if you are instantiating gameObjects you will have to reference it other ways. One way is to store the instantiated gameObject into a public variable. Then you can access it from other scripts. Do some searching for your specific needs but the answers are already in the forums.
Again, findwithtag and storing it for future reference is fine for a single object. But if you start instantiating a lot of objects with the same tag even a single call could hinder performance so It’s good to know other methods.
Also, if you have stats that need to be accessed by other gameObjects or Events I would recommend using a gameManager to manage those stats. You then use the gameManager like a singleton class to keep track of your player stats. Then if some interaction arises where a NPC or other object needs to access a certain stat its as easy as referencing your gameManager. It’s best to implement things like this before making a large protion of the game obviously.