OK, so my feeling is this should be a straight forward but for whatever reason I am getting an odd result. The idea is that on a button press; a boolean variable is changed from false to true. However, when I go to check the value of the bool (after pressing the button) using a debug.log in an update function it returns false, false, false, true every frame. The hope is to use the value as a breaker for a while loop in another part of the code, but I think I need it to be constant true to do so…
Any help would be much appreciated! Also, I can post more of the source code if desired, although the variable isn’t in use anywhere else currently.
private bool stop = false;
void Update ()
{
Debug.Log(stop);
}
public void Stop ()
{
stop = true;
}
Edit: Here is the code in full =]
The idea is to collect location data to write to txt, with start (track), stop (Stop) and reset (Reset) buttons.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Text;
using UnityEngine.UI;
public class countWithStop : MonoBehaviour {
private int c = 1; // counter used for while time loop
private int place = 0; // counter used for tracking places
private string filepath = "/locationdata.txt"; // the designated filepath that location data is written to
private FileInfo file; // creating an info point for files
private string placeName; // String of the place numbers for easy wrtiting to .txt
private string time; // String that holds the time and date
private float longitude; // current longitude
private float latitude; // current latitude
private string location; // used to format the info for the text document
private StreamWriter locationData; // creates the .txt
public GameObject timer; // countdown timer object
public Text currTimer; // countdown timer text
private int K = 0; // variable used for timer calculation
private bool running = false;
private bool stop = false;
void Awake ()
{
Application.targetFrameRate = 30;
}
void Start ()
{
//file = new FileInfo(Application.persistentDataPath + filepath);
file = new FileInfo(Application.dataPath + filepath);
PlayerPrefs.SetInt("countrack", K);
}
// Update is called once per frame
void Update ()
{
//latitude = Input.location.lastData.latitude;
//longitude = Input.location.lastData.longitude;
Debug.Log(stop);
latitude = 1;
longitude = 2;
}
public void Track () // function called on button press
{
c = 0;
if (place < PlayerPrefs.GetInt ("place")) {
place = PlayerPrefs.GetInt ("place");
}
place += 1;
PlayerPrefs.SetInt ("place", place);
if (!file.Exists) {
locationData = file.CreateText ();
} else {
locationData = file.AppendText();
}
timer.SetActive(true);
StartCoroutine (Counter());
}
IEnumerator Counter ()
{
running = true;
while (c < 121/*241*/) { // the amount of half seconds needed for desired time
if (c % 2 == 0) {
K = 60 - (c / 2); // 60 seconds test
currTimer.text = K.ToString ();
//Debug.Log (K);
} else {
K = 60 - ((c - 1) / 2); // should be 120
currTimer.text = K.ToString ();
}
placeName = place.ToString ();
time = DateTime.UtcNow.ToString ();
c += 1;
location = placeName + " " + time + " " + longitude + " " + latitude;
locationData.WriteLine (location);
//Debug.Log(location);
yield return new WaitForSeconds (0.5f);
}
timer.SetActive(false);
locationData.Close();
running = false;
}
public void Reset ()
{
Stop ();
place = 0;
PlayerPrefs.DeleteAll ();
file.Delete ();
}
public void Stop ()
{
stop = true;
}
}