Write to a file after a button has been pressed?,How to write to a file after pressing a button?
I have created a stop watch using text. I have a button when pressed stops the stopwatch. I want to store that time of the stop watch when stopped in a text file?
this is the stopwatch code - using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class StopWatch : MonoBehaviour { float timer; float seconds; float minutes; float hours;
bool start;
[SerializeField] Text StopwatchText;
// Start is called before the first frame update
void Start()
{
start = true;
timer = 0;
}
// Update is called once per frame
void Update()
{
StopWatchCalcul();
}
void StopWatchCalcul()
{
if (start)
{
timer += Time.deltaTime;
seconds = (int)(timer % 60);
minutes = (int)((timer / 60) % 60);
hours = (int)(timer / 3600);
StopwatchText.text = hours.ToString("00") + ":" + minutes.ToString("00") + ":" + seconds.ToString("00");
}
}
public void stopTimer()
{
start = false;
}
},I have a stop watch created that stops when I press a button. I would like to store the time of the stopwatch to a file when the button is pressed?
This is the code for the stopwatch. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class StopWatch : MonoBehaviour { float timer; float seconds; float minutes; float hours;
bool start;
[SerializeField] Text StopwatchText;
// Start is called before the first frame update
void Start()
{
start = true;
timer = 0;
}
// Update is called once per frame
void Update()
{
StopWatchCalcul();
}
void StopWatchCalcul()
{
if (start)
{
timer += Time.deltaTime;
seconds = (int)(timer % 60);
minutes = (int)((timer / 60) % 60);
hours = (int)(timer / 3600);
StopwatchText.text = hours.ToString("00") + ":" + minutes.ToString("00") + ":" + seconds.ToString("00");
}
}
public void stopTimer()
{
start = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612195529im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Not writing to file correctly 1 Answer
WebRequest in unity 1 Answer
UI image sorting trought depth/render queue? 0 Answers
Adding Shader Graph to normal project 0 Answers
A problem with a shader to make a window transparent 0 Answers