• 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 Omwro · Sep 21, 2016 at 02:12 PM · buttonvalue

c# problem with changing value with button click

i have found a weird bug or im making a mistake somewhere. i'll explain my problem here: i made a bool in my public class for example"bool easy;". then i wrote in void Start()"easy = false;". i also did add in void update() print to control it"print(easy)". i also have a NGUI button called EasyButton and it works so there is no problem with the button. here the code of the button:"public void EasyButton(){ easy = true; }" but when i run the game and check the print 20% says true and 80% says false. i really dont know why and how its possible that why i think it is a bug. also sorry for my bad english.

an example of the code: bool easy;

void Start(){ easy = false; }

void Update(){ print(easy); }

public void EasyButton(){ easy = true; }

ill send down below the code in my script so maybe it is for you guys easier to find a solution:

 using System;
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class lvl7script : MonoBehaviour {
     public bool Jump = false;
     public bool Grounded = true;
 
     public bool left;
     public bool mid;
     public bool right;
     public bool pressed;
 
     GameObject Panel;
     public float speed;
 
     bool easy;
     bool normal;
     bool hard;
 
     // Use this for initialization
     void Start () {
         Time.timeScale = 0;
         Panel = GameObject.Find("Panel");
         easy = false;
         normal = false;
         hard = false;
     }
 
     // Update is called once per frame
     void Update() {
         if (Application.loadedLevel == 7)
         {
             if (Time.timeScale == 1)
             {
                 print(easy);
                 print(normal);
                 print(hard);
                 if (easy)
                 {
                     speed = 0.3f;
                     Panel.GetComponent<Canvas>().enabled = false;
                 }
                 if (normal)
                 {
                     speed = 0.6f;
                     Panel.GetComponent<Canvas>().enabled = false;
                 }
                 if (hard)
                 {
                     speed = 0.9f;
                     Panel.GetComponent<Canvas>().enabled = false;
                 }
                 //controls
                 transform.Translate(0, 0, speed);
                 transform.Translate(0, Time.deltaTime, 0, Space.World);
 
                 //movement from 1 to other lane
                 if (mid == true && Input.GetKeyDown(KeyCode.LeftArrow) && !pressed)
                 {
                     transform.position = new Vector3(-5, (transform.position.y), (transform.position.z));
                     mid = false;
                     left = true;
                     right = false;
                     pressed = true;
                 }
                 if (mid == true && Input.GetKeyDown(KeyCode.RightArrow) && !pressed)
                 {
                     transform.position = new Vector3(5, (transform.position.y), (transform.position.z));
                     mid = false;
                     left = false;
                     right = true;
                     pressed = true;
                 }
                 if (left == true && Input.GetKeyDown(KeyCode.RightArrow) && !pressed)
                 {
                     transform.position = new Vector3(0, (transform.position.y), (transform.position.z));
                     mid = true;
                     left = false;
                     right = false;
                     pressed = true;
                 }
                 if (right == true && Input.GetKeyDown(KeyCode.LeftArrow) && !pressed)
                 {
                     transform.position = new Vector3(0, (transform.position.y), (transform.position.z));
                     mid = true;
                     left = false;
                     right = false;
                     pressed = true;
                 }
                 if ((Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow)) && pressed)
                 {
                     pressed = false;
                 }
 
                 //jump control
                 if (Jump == false && Input.GetKeyDown("up"))
                 {
                     Vector3 jump = new Vector3(0.0f, 400.0f, 0.0f);
                     GetComponent<Rigidbody>().AddForce(jump);
                     Jump = true;
                     Grounded = false;
                 }
                 //stay on 1 lane 
                 if (mid == true && transform.position.x != 0)
                 {
                     transform.position = new Vector3(0, (transform.position.y), (transform.position.z));
                 }
                 if (left == true && transform.position.x != -5)
                 {
                     transform.position = new Vector3(-5, (transform.position.y), (transform.position.z));
                 }
                 if (right == true && transform.position.x != 5)
                 {
                     transform.position = new Vector3(5, (transform.position.y), (transform.position.z));
                 }
             }
         }
     }
 
     //collision for grounded
     void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.name == "Ground")
         {
             Grounded = true;
             Jump = false;
         }
     }
     public void EasyButton()
     {
         Time.timeScale = 1;
         easy = true;
     }
     public void NormalButton()
     {
         Time.timeScale = 1;
         normal = true;
     }
     public void HardButton()
     {
         Time.timeScale = 1;
         hard = true;
     }
 }

here is a screenshot of the print result: http://prntscr.com/ck5u9u

Comment
Add comment · Show 1
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 Omwro · Sep 21, 2016 at 02:23 PM 0
Share

does nobody know a solution? i know a bit where it goes wrong but i don't know how to fix it in a right way

3 Replies

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

Answer by Omwro · Sep 22, 2016 at 01:08 PM

Well i found a solutin after some days of research. i knew there was something wrong with attaching the script to multiple gameobjects. i finally found a solution. i was slovenly with the code and made mistakes with the if statements and the debug log(print). i saw when i run the game and pause it. that only the components of the button which i pressed did change but the other attached buttons didnt so i added this: using System; using UnityEngine; using UnityEngine.UI; using System.Collections;

 public class lvl7script : MonoBehaviour
 {
     GameObject Panel;
     GameObject Ebutton;
     GameObject Nbutton;
     GameObject Hbutton;
  
     public int difficulty;
 
     void Start()
     {
         Ebutton = GameObject.Find("EasyButton");
         Nbutton = GameObject.Find("NormalButton");
         Hbutton = GameObject.Find("HardButton");
     }
 
     void Update()
     {
                 Debug.Log(Hbutton.GetComponent<lvl7script>().difficulty);
                 if (Ebutton.GetComponent<lvl7script>().difficulty == 1)
                 {
                     speed = 0.5f;
                     Panel.GetComponent<Canvas>().enabled = false;
                 }
                 if (Nbutton.GetComponent<lvl7script>().difficulty == 2)
                 {
                     speed = 1f;
                     Panel.GetComponent<Canvas>().enabled = false;
                 }
                 if (Hbutton.GetComponent<lvl7script>().difficulty == 3)
                 {
                     speed = 1.5f;
                     Panel.GetComponent<Canvas>().enabled = false;
                 }
     }
     public void EasyButton()
     {
         Time.timeScale = 1;
         difficulty = 1;
     }
     public void NormalButton()
     {
         Time.timeScale = 1;
         difficulty = 2;
     }
     public void HardButton()
     {
         Time.timeScale = 1;
         difficulty = 3;
     }
 }

i tried in the if statement to only look to the gameobject component and that fixed it.

Comment
Add comment · 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
0

Answer by Leoo · Sep 21, 2016 at 02:54 PM

Actually your code is OK, for the most part. But your doing something wrong in terms of basic debug principles.

If you are doing this:

           print(easy);
           print(normal);
           print(hard);


Now, you are expecting to see what is going on in your code by trying to debug with those prints. How would you be able to actually KNOW which one of those prints is the variable you are looking for , IF you are currently printing "unknown' data into the log?.

Try this:

 Debug.Log("Easy :" + easy);
 Debug.Log("Normal:" + normal);
 Debug.Log("Hard :" + hard);

https://docs.unity3d.com/ScriptReference/Debug.Log.html

Comment
Add comment · Show 1 · 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 Omwro · Sep 21, 2016 at 03:08 PM 0
Share

first of all, thanks for your reply. i understand what you try to say and i know i write most of the codes slovenly but it didn't fix the code. here is a screenshot of the console: http://prntscr.com/cknw3o. when i click the Easy button only a part is true and the other part is false. i think i found the problem. i did attach the script to mutiple buttons(EasyButton, NormalButton, HardButton) and when i disable the script of the buttons, the part in void update: if (easy){} won't work. do you have any solution for this? thank you in advance.

avatar image
0

Answer by Bonfire-Boy · Sep 21, 2016 at 03:22 PM

First thing you need to do is turn off the "Collapse" option in your console so you can see what order these things are happening.

Also check that the script isn't on more than one active gameobject and/or add the gameobject's name to the debug.log so you know which one is logging what if there are multiple instances.

And note that the way you're using those easy/hard flags looks dodgy because they can all end up set to true. To make them mutually exclusive you either want to set the others to false whenever you set one to true or (better) use an enum.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Ramp Offset with Button press 1 Answer

Rotation Code Help 3 Answers

Is it a bad idea to add and remove listeners for toggles and buttons at runtime? 2 Answers

Why is checking button click in Unity done this way? 1 Answer

Show UI Button press color from script when invoking onClick 2 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