- Home /
Question by
KubaSzewczyk · Mar 08, 2017 at 09:18 AM ·
scripting beginnerplayerprefstimerscene-loadingtimer-script
Not sure what is wrong with my timer script.
Just need someone to look through and check what is wrong. The script is supposed to start a timer once the scene with this script is loaded, and when OnTriggerEnter2D is pressed, save the time (preferably in PlayerPrefs) and reset the timer. Once this process has happened nine times, it loads up a new scene and displays all of the times. Very much a beginner, so any help is really appreciated. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Timer : MonoBehaviour {
private List<float> timers;
private float lastClickTime = -1;
private void Start()
{
timers = new System.Collections.Generic.List<float>();
}
private void OnTriggerEnter2D (Collider2D other)
{
timers.Add(lastClickTime > 0 ? Time.time - lastClickTime : Time.time);
if (timers.Count == 9)
{
PlayerPrefs.SetFloat("RTime", 0f + Time.time);
print(PlayerPrefs.GetFloat("RTime"));
SceneManager.LoadScene("GamePanel", LoadSceneMode.Single);
}
}
}
Comment
Answer by ExtinctSpecie · Mar 09, 2017 at 12:48 PM
so you can try something like this
float timer;
public static List<float> timers;
int maxTriggers = 9;
void Start()
{
timers = new List<float> ();
}
void Update()
{
timer += Time.deltaTime;
}
void OnTriggerEnter2D (Collider2D other)
{
timers.Add (timer);
timer = 0f;
if (timers.Count >= maxTriggers)
{
//do your thing
}
}
public static List<float> getTimers()
{
return timers;
}
Your answer
Follow this Question
Related Questions
Loading a random scene/level. 1 Answer
I'm not sure what is wrong with my script 0 Answers
How do I stop my timer when all of the "dummies" are destroyed? 2 Answers
PlayerPrefs & Time.time 1 Answer
Speed up timer 1 Answer