- Home /
Changing a boolean value on button press not acting as expected?
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 + "\t" + time + "\t" + longitude + "\t" + 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;
}
}
Yes, we need more code. There's no logic shown here, so no errors either. ;) It usually helps to try and isolate the issue as much as possible, starting with a clean scene, only adding a single script, then testing and so on.
Answer by Xarbrough · Feb 22, 2018 at 03:49 PM
So the issue is still not in this code. You may have multiple instances of your script running at the same time though. Have you created an empty test scene, deleted all objects, added a single object with the script on it and tested again? You said, it was printing "false, false, false, true every frame", but that wouldn't work unless you had 4 instances of the script running, right?
Ah, I did indeed have four instances of the script running, not something I'd really thought about/ noticed before, so thanks! In that case, I think I need to rethink the way I'm scripting for multiple buttons in a project.. unless variable value changes are saved across instances of the same script?
Thanks a lot for the help! I really appreciate it =] I've managed to iron out some of the problems I was having thanks to your observations.