- Home /
Problem with if statement logic- Need help!
I'm trying to get my main menu working properly. I have three chapter selectors. When selected, each will active a different camera with the proper chapter backgrounds. Each chapter's camera's culling masks are set to only see the GUITexture for that specific chapter's levels. This works great. Back button brings me back to the main menu. it's all working how I want. problem is, no matter what method I use, i cannot get the GUITexture buttons to STOP WORKING. See>
In Main Menu- Main Menu Buttons(Yes)/Chapter1 Buttons(No)/Chapter2 Buttons (No)/Chapter3 Buttons(no)
In Chapter 1- Chapter1 (yes)/Main Menu Buttons(no)/Chapter2 (no)etc....
In Ch2- Ch 2(yes)/Main M(no)/CH1 (no)/etc...
That logic seems sound, right? But whether I use guiTexture.enabled=true/false; NO effect. Ok, just wrap the level select code in an if statement right? such as if(canClickCH1==true){ Allow clicking of chapter 1 levels }
DOESN'T WORK. No matter how I arrange my If statements. The code below is the code I use for Chapter 1 level select. (Same for chapter 2, but the variable names are changed) yet it's not working. At all. There's got the be something wrong with my logic somewhere. Can someone help me figure this out? It's been driving me crazy for days. thanks for your time. I just want to disable all the GUITexture buttons I'm not suing in the moment and only be able to click the ones I do want to use. (This is messy code with a lot blocked out. That's either for testing ir something I haven't deleted yet. Sorry.)
public var IsChapter : int;
public var Ch1unlocked : boolean = false;
var levelTex : Texture2D;
//static var chooseCHlvl : int;
public var levelToLoad : int;
static var can1 : boolean = false;
public var canClick : boolean;
public var canGo : boolean = false;
var Camera2 : Camera;
function Start () {
can1=false;
guiTexture.enabled = false;
}
function Update () {
canClick = can1;
guiTexture.texture = levelTex;
AutoResize(1920, 1080);
/*if(thisLevel == PlayerPrefs.GetInt("CH1lvl2")){
Ch1unlocked = true;
}
if(thisLevel == PlayerPrefs.GetInt("CH1lvl3")){
Ch1unlocked = true;
}
if(thisLevel == PlayerPrefs.GetInt("CH1lvl4")){
Ch1unlocked = true;
}
if(thisLevel == PlayerPrefs.GetInt("CH1lvl5")){
Ch1unlocked = true;
}
if(thisLevel == PlayerPrefs.GetInt("CH1lvl6")){
Ch1unlocked = true;
}
if(thisLevel == PlayerPrefs.GetInt("CH1lvl7")){
Ch1unlocked = true;
}
*/
if(can1 == true){
guiTexture.enabled = true;
for (var i = 0; i < Input.touchCount; ++i)
{
if(guiTexture.HitTest(Input.GetTouch(i).position)){
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
if(Input.GetTouch(i).tapCount == 1)
{
//if(can1==true){
can1=false;
chooseLevel();
// }
}
}
}
}
}
/* if (guiTexture.HitTest(Input.mousePosition)) {
if(can2 == true){
choose2Level();
}
if(can1 == true){
chooseLevel();
}
}*/
}
public static function AutoResize(screenWidth:int, screenHeight:int):void
{
var resizeRatio:Vector2 = Vector2(Screen.width / parseFloat(screenWidth), Screen.height / parseFloat(screenHeight));
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3(resizeRatio.x, resizeRatio.y, 1.0));
}
/*function FadeIn(){
//yield Fade(0.0, 1.0, 1);
if(Ch1unlocked==true){
yield Fade(0.0, 1.0, 1);
}
if(Ch1unlocked==false){
yield Fade(0.0, 0.5, 2.0);
}
}*/
function chooseLevel(){
can1=false;
Application.LoadLevel (levelToLoad);
}
function FadeUnlocked(){
yield Fade(0.0, 1, 1);
}
function Fade (startLevel :float, endLevel :float, duration :float) {
var speed : float = 0.3/duration;
for (var t :float = 0.0; t < 1.0; t += Time.deltaTime*speed) {
guiTexture.color.a = Mathf.Lerp(startLevel, endLevel, t);
yield;
}
}
The code you've posted is really messy, and it's kinda hard to get a grip on it. I'd recommend removing the commented out stuff, and get your IDE (like $$anonymous$$onodevelop) to indent your code properly!
It's also not quite clear what the problem is - does the GUI texture of the buttons not disappear when you disable the button? Or are they invisible but clickable?
Agreed with comment above, rephrase your question and repost simplified version of your code please! Wishing you luck.
Sorry Lads. Cleaned up Code below. $$anonymous$$y problem is not that they go invisible. (using the culling masks eli$$anonymous$$ates the need for that) The problem is that they're clickable when they shouldn't be. When I'm in the main menu, I can still click the level buttons. When I'm choosing a level, I can still click the chapter buttons. When I shouldn't be able to. Thanks for the help guys.
var levelTex : Texture2D;
static var chooseCHlvl : int;
public var levelToLoad : int;
static var can1 : boolean = false;
public var canClick : boolean;
var Camera2 : Camera;
function Start () {
can1=false;
guiTexture.enabled = true;
}
function Update () {
canClick = can1;
guiTexture.texture = levelTex;
guiTexture.enabled = true;
for (var i = 0; i < Input.touchCount; ++i)
{
if(guiTexture.HitTest(Input.GetTouch(i).position)){
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
if(Input.GetTouch(i).tapCount == 1)
{
if(can1 == true){
can1=false;
chooseLevel();
}
}
}
}
}
}
function chooseLevel(){
can1=false;
Application.LoadLevel (levelToLoad);
}
function FadeUnlocked(){
yield Fade(0.0, 1, 1);
}
function Fade (startLevel :float, endLevel :float, duration :float) {
var speed : float = 0.3/duration;
for (var t :float = 0.0; t < 1.0; t += Time.deltaTime*speed) {
guiTexture.color.a = $$anonymous$$athf.Lerp(startLevel, endLevel, t);
yield;
}
}
Answer by Habitablaba · Sep 26, 2014 at 10:53 PM
Here is a collapsed and reformatted version of your checks from the Update method.
for (var i = 0; i < Input.touchCount; ++i)
{
if(guiTexture.HitTest(Input.GetTouch(i).position)
&& Input.GetTouch(i).phase == TouchPhase.Began
&& Input.GetTouch(i).tapCount == 1
&& can1)
{
can1=false;
chooseLevel();
}
}
It looks to me like this should work, which causes me to look elsewhere.
Namely:
static var can1 : boolean = false;
Why is this static? Where else is this being set?
When the "Chapter 1" button is pressed from the main menu, it's called from the mainGUI script I use and can1 is supposed to be set to true. Then if you go back from the level select screens to the $$anonymous$$ain menu (switching cameras), it is set to false. Which works great. For both Chapter 1 and 2. Which is why I'm so confused as to why it's not working.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with acclerometer 0 Answers
Problem with jcar script for android mobile 1 Answer
Pause Android game- menu pop-up? 1 Answer