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 /
  • Help Room /
avatar image
0
Question by FuryFight3r · Jan 16, 2017 at 01:29 PM · guitextmultipleguitextoverlap

GUI Text showing even if the 'if' statement is not met

Hey guys, having an issue where i have a very.... very basic Task handler, so what happens is if the phone is ringing 'isRinging = true;' and i have a GUI that is asking if isRinging = true then to display this text, then another bool for is hasAnswered then to display the next task... for some reason the isRinging text still appears, blocking the view of half the next task. i did use to have this working, and dont remember changing anything to make it work otherwise, but now it is doing it and cant fix it... tried adding more statements into the if statement like && !hasAnswered but just keeps displaying the text. here is my code I'm trying to use, i know its messy, but thats what happens when something goes wrong and you do a heap of random shit to try and fix it... with no avail. even changed them from all if statements to else if and still no avail.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PhoneScript : MonoBehaviour {
 
     public AudioSource phoneRinging;
     public AudioSource phoneCall;
     public BoxCollider boxCollider;
     public Animator rampAnim;
     public Animator switchAnim;
     bool hasAnswered;
     bool isRinging;
     bool rampActivated;
 
     // Use this for initialization
     void Start () {
         phoneRinging.enabled = true;
         phoneCall.enabled = false;
         hasAnswered = false;
         isRinging = true;
         rampAnim.enabled = false;
         rampActivated = false;
         switchAnim.enabled = false;
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void OnGUI()
     {
         if (isRinging && !hasAnswered)
         {
             GUI.skin.label.alignment = TextAnchor.UpperLeft;
             GUI.Label(new Rect(10, 10, 1000, 400), "Task: Answer The Phone.");
         }else
         if (hasAnswered && !isRinging)
         {
             GUI.skin.label.alignment = TextAnchor.UpperLeft;
             GUI.Label(new Rect(10, 10, 1000, 400), "Task: Get to the boat and find a way to get your car off.");
         }else
         if (rampActivated)
         {
             GUI.skin.label.alignment = TextAnchor.UpperLeft;
             GUI.Label(new Rect(10, 10, 1000, 400), "Task: Make your way around the island.");
         }
     }
 
     public void answerPhone()
     {
         phoneRinging.enabled = false;
         phoneCall.enabled = true;
         isRinging = false;
         hasAnswered = true;
         boxCollider.enabled = false;
     }
 
     public void activateRamp()
     {
         rampAnim.enabled = true;
         switchAnim.enabled = true;
         rampActivated = true;
     }
 }
 
Comment
Add comment · Show 13
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 FuryFight3r · Jan 16, 2017 at 03:22 AM 0
Share

damn this wait time is getting longer and longer -.- 2 days of wait time now... are the mods on here just trolls or is there a legitimate reason that they cannot maintain a forum/answers?

avatar image ScaniX · Jan 16, 2017 at 02:07 PM 0
Share

Did you do the usual debugging like a Debug.Log(GetInstanceID()) to make sure there is only one instance of this script? Or breakpoints to make sure that everything is called in the expected order?

avatar image FuryFight3r ScaniX · Jan 17, 2017 at 10:38 AM 0
Share

i dont understand what you mean, yes there is only one of this script on one object... the phone, and i don't see, nor understand how debugging is going to help me find what this type of issue is.... and by break points do you mean..?

          if (isRinging)
          {
              GUI.skin.label.alignment = TextAnchor.UpperLeft;
              GUI.Label(new Rect(10, 10, 1000, 400), "Task: Answer The Phone.");
          }else{
              break;  //this??
          }

and does breaking simply mean it cancels out the if statement if the requirements are not met? and if this is the case, wont it break all my if statements under GUI? or only if isRinging is not met?

maybe some more information might enlighten me on what you are asking me to try.

PS. I'm relatively new to Game Coding if you couldn't already tell by me not knowing what you have asked.

avatar image ScaniX FuryFight3r · Jan 17, 2017 at 11:36 AM 0
Share

Debugging is the one and only thing that helps you find out what the problem is. That is what debugging is for. When you coded for a while, you will learn to love breakpoints and even more: Printouts. ;)

A breakpoint is a line where the execution halts to let you inspect the situation you are in with all variables etc. you can set a breakpoint in Visual Studio with F9 (I think).

Both of the instance printout and the breakpoints (or more printouts of your booleans) will help you verify:

  • there is only one instance (as you believe)

  • the code is executed in the expected order

There won't be two texts displayed at the same time if the above is right as OnGUI() is only displaying one text at a time.

Show more comments
avatar image FuryFight3r · Jan 17, 2017 at 03:13 PM 0
Share

even tried adding a simple if statement to the update void, of if hasAnsered = true then set is Ringing to false, and still shows up..

avatar image ScaniX FuryFight3r · Jan 17, 2017 at 03:18 PM 0
Share

Yes. In the script instance where you enter that IF, the variable isRinging is already false.

avatar image ScaniX · Jan 17, 2017 at 05:24 PM 0
Share

To access a component of another gameobject from within a script you can either add a public variable (like public GameObject theOtherOne;) to your script and assign the corresponding gameObject in the inspector or you can get it in the Start() method using GameObject.Find().

1 Reply

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

Answer by FuryFight3r · Jan 17, 2017 at 06:30 PM

Found that the object was indeed on a secondary object, that i had the script active on to allow that certain object to function how i wanted, have re-coded the interact script to suit.

i had this issue by the interact script i was using trying to access the hit.collider of the Ramp switch, when in actual fact the script was on the phone, this is the modification i had made.

 public GameObject phone;
     public void Update ()
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, out hit, interactDistance, interactLayer))
         {
             if (isInteracting == false)
             {
                 if (interactIcon != null)
                 {
                     curser.enabled = false;
                     interactIcon.enabled = true;
                     if (Input.GetKeyDown(KeyCode.E))
                     {
                       if (hit.collider.CompareTag("Phone"))
                         {
                             hit.collider.GetComponent<PhoneScript>().answerPhone();
                         }
                         else if (hit.collider.CompareTag("Ramp"))
                         {
                           //CHANGED TO phone.GetComponent and added a public GameObject to the Phone to access it, rather than trying to access the hit.collider//
                             phone.GetComponent<PhoneScript>().activateRamp();
                         }
                    }
             }
      }
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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gui Text won't turn off 0 Answers

Cannot see Text 1 Answer

TextUI text changes size if resolution changes 1 Answer

GUI Text not showing up :( 7 Answers

How to get tooltips to pop up on certain levels, but not all of them 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