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;
}
}
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?
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?
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.
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.
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..
Yes. In the script instance where you enter that IF, the variable isRinging
is already false.
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().
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();
}
}
}
}
Your answer
Follow this Question
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