Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Issues with my Stamina Regeneration Script

Discussion in 'Scripting' started by AnnoyingGames, Mar 13, 2018.

  1. AnnoyingGames

    AnnoyingGames

    Joined:
    Nov 9, 2014
    Posts:
    9
    What I'm trying to do :

    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


    Part 2: Issue

    if (GameObject.FindGameObjectWithTag("Player_Joe") && isActiveAndEnabled == true)
    {
    if(Input.GetKeyDown(KeyCode.Space) && pController.isGrounded == true && playerStamina != 0 && canJump == true)
    {
    staminaBar.value = (playerStamina - costToJump);
    playerStamina = (playerStamina - costToJump);
    pController.pRigidBody.velocity = new Vector2(0, pController.jumpHeight);
    }
    }

    if (GameObject.FindGameObjectWithTag("Player_Joe") && isActiveAndEnabled == true)
    {
    if (playerStamina < 30)
    {
    canJump = false;
    }
    else if (playerStamina >= 30)
    {
    canJump = true;
    }

    if (pController.isGrounded == true)
    {
    canRegenStamina = true;
    if (playerStamina <= 0 && canRegenStamina == true)
    {
    playerStamina = 0;
    }
    if (playerStamina != 300)
    {
    do
    {
    playerStamina = (playerStamina + regenStaminaAmount);
    regenStaminaTime = (regenStaminaTime + 0.1f);
    } while (regenStaminaTime <= 3600.0f);

    if (regenStaminaTime == 3600.0f && playerStamina == 300)
    {
    regenStaminaTime = 0.0f;
    }
    }
    } else
    {
    canRegenStamina = false;
    }
    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    3,026
    Your code is getting cut off. Is part 2 your entire Update?
    Post code on the forums with Code tags:
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    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?
     
    StickyHoneybuns likes this.
  3. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    186
    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.

    https://forum.unity.com/threads/using-code-tags-properly.143875/

    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.
     
  4. AnnoyingGames

    AnnoyingGames

    Joined:
    Nov 9, 2014
    Posts:
    9
    Nvm figured it out lol :D
     
  5. AnnoyingGames

    AnnoyingGames

    Joined:
    Nov 9, 2014
    Posts:
    9
    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?
     
    Last edited: Mar 13, 2018
  6. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    186
    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.

    Good luck and Happy Gaming!
     
  7. AnnoyingGames

    AnnoyingGames

    Joined:
    Nov 9, 2014
    Posts:
    9
    Thanks buddy, I'll take a look into that. ^^
     
    StickyHoneybuns likes this.