Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by GrimFunko · Feb 22, 2018 at 03:19 PM · updatebooleanwhile-loopbreak

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;
     }
 }
 
Comment
Add comment · Show 2
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 Xarbrough · Feb 22, 2018 at 03:25 PM 0
Share

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.

avatar image GrimFunko Xarbrough · Feb 22, 2018 at 03:37 PM 0
Share

ah haha, sorry about that!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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?

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 GrimFunko · Feb 22, 2018 at 05:14 PM 0
Share

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?

avatar image GrimFunko · Feb 23, 2018 at 11:01 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

75 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

Related Questions

How to make this code fire projectile once during the Update function? I know Update means every frame. 1 Answer

Making a level up button., 2 Answers

while Looping 1 Answer

Boolean being constantly redefined? 2 Answers

Boolean From Another Class Not Being Updated 3 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