Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Feb 15, 2021 at 07:32 PM by Hellium for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Radical_boy · Feb 15, 2021 at 07:32 PM · timeplayerprefslives

How do i make a script that re-fills lives after 10min?

EDIT i got the life going down mechanics, i just need the time script. Everytime deaths go to 5 in a level i want to lower the PlayerPrefs for the Big Lives(ones in the menu, that decide wether you can play or not) by one. I dont know how to just decrease it instead of setting the PlayerPrefs to a specific number. Also if Big Lives <= 5 i want a timer to start that of reaches 10 min it would replenish one Big Life. But i cant really find anything. Can anybody help?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Box_Frog · Feb 15, 2021 at 06:26 PM

If you just want to decrease the number by one you could do: bigLives--; or if you want to decrease by a certain number then you do this: bigLives-= 2; which subtracts two from the value.

For the timer I usually make something like this with the startLifeTimer being the amount between each live. Then subracting one second from lifeTimer until it equals 0, where we add a life and reset the timer.

 float startLifeTimer;
 float lifeTimer;

  void Start
  {
      lifeTimer = startLifeTimer;
  }
 
 if(bigLives <= 5)
      if(lifeTimer < 0)
      {
            bigLives++;
            PlayerPrefs.SetFloat("BigLives", bigLives);
            lifeTimer = startLifeTimer;
      }
      else
      {
         lifeTimer -= Time.deltaTime;
      }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Radical_boy · Feb 15, 2021 at 06:32 PM 0
Share

Is there a way i could store bigLives in PlayerPrefs? I said i got it but it didnt work as i wanted. Also, on the last row was there supposed to be lifeTimer -=Time.deltaTime?

avatar image Box_Frog Radical_boy · Feb 15, 2021 at 06:43 PM 0
Share

I edited my answer. This is how I would store it in PlayerPrefs and yea the last line was just a typo meant to put lifeTimer.

avatar image Radical_boy Box_Frog · Feb 15, 2021 at 07:03 PM 0
Share

I will try it tommorow as its linda late here. I guess startLifeTimer should be the time I want it to take for a life to replenish?

Show more comments
avatar image
1

Answer by logicandchaos · Feb 15, 2021 at 06:39 PM

use coroutines, so something like this:

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour
 {
     void Start()
     {
         StartCoroutine(ResetLives);
     }
 
     private IEnumerator ResetLives()
     {
         while(true)
         {
             yield return new WaitForSeconds(10f);
             player.lives=10;
         }
     }
 }

More info on coroutines:

https://docs.unity3d.com/ScriptReference/Coroutine.html

https://stackoverflow.com/questions/53139259/making-a-timer-in-unity

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Radical_boy · Feb 15, 2021 at 06:59 PM 0
Share

Is this C#? It looks kinda different from what ive seen until now. I will try it tommorow as its kinda late here, thx

avatar image AbandonedCrypt Radical_boy · Feb 15, 2021 at 08:12 PM 0
Share

Yes it is C#, this is a common unity functionality to execute iterative code outside of the main Update() loop

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save player's lives using playerprefs while I load the same scene? 2 Answers

Help With Quickest TimeScore 0 Answers

Save best time 1 Answer

How to save and display players times using Playerprefs in Javascript? 1 Answer

PlayerPrefs, First time applying and need help. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges