Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Turning blue in the face : Bool

Discussion in 'Scripting' started by StarShipMan, Jul 2, 2014.

  1. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    Hi everyone, been researching until i turned blue in the face...im still blue....

    I cant seem to get the reference to a bool no matter what i do. I have a LevelSelectState setup, so if i click on a specific button on the screen i get a bool to fire true on a component. The true bool loads a level basically.

    For all of my buttons i was able to use GetComponent (to get a ref. to the component) and it worked fine. Now i have to use AddComponent on another button as GetComponent doesnt work, however now i cant get the bool reference on the script im trying to call.

    My Button Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseButton_Level_1_11 : MonoBehaviour
    5. {
    6.     public GUITexture guiTex1;
    7.     public GUITexture guiTex2;
    8.    
    9.     public bool levelOneDashElevenGameButton = false;
    10.    
    11.     void Start()
    12.     {
    13.     }
    14.    
    15.     void OnMouseDown()
    16.     {
    17.         Debug.Log ("The Mouse is down" + this.name);
    18.         guiTex1.enabled = false;
    19.         guiTex2.enabled = true;
    20.         audio.Play ();
    21.     }
    22.    
    23.     void OnMouseUp()
    24.     {
    25.         guiTex2.enabled = false;
    26.         guiTex1.enabled = true;
    27.         Debug.Log ("The Mouse is up" + this.name);
    28.         levelOneDashElevenGameButton = true;
    29.     }
    30. }
    Level Select State Script (calling the bool levelOneDashElevenGameButton):

    Code (CSharp):
    1. //Level 1_11 Button Logic
    2.             GameObject level1_11Obj;
    3.             level1_11Obj = GameObject.Find("Button_Level_1_11");
    4.             if(level1_11Obj == null)
    5.             {
    6.                 Debug.Log ("level1_11Obj is NOT available in LevelSelectState");
    7.                 level1_11Obj = GameObject.FindWithTag("Button_Level_1_11");
    8.                 if(level1_11Obj != null)
    9.                 {
    10.                     Debug.Log ("level1_11Obj is NOW available in LevelSelectState");
    11.                     level_1_11 = level1_11Obj.AddComponent<MouseButton_Level_1_11>();
    12.                     if(level_1_11 != null)
    13.                     {
    14.                         Debug.Log ("level_1_11 is available in LevelSelectState");
    15.                         if(level_1_11.levelOneDashElevenGameButton) // Cannot get this reference from my MouseButton Script!!!
    16.                         {
    17.                             Debug.Log("You have selected Level 1_11");
    18.                             Time.timeScale = 1;
    19.                             manager.SwitchState (new PlayStateScene_11 (manager));
    20.                         }
    21.                     }
    22.                     else
    23.                         Debug.Log("level_1_11 is NOT available in LevelSelectState");
    24.                 }
    25.             }
    26.             else
    27.                 Debug.Log ("level1_11Obj is available in LevelSelectState");
    Any assistance would be greatly appreciated!!!
     
  2. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    So it prints "level_1_11 is available in LevelSelectState" then nothing?
    Then it's normal.When you add a new MouseButton_Level_1_11 it's field levelOneDashElevenGameButton equals to false and nothing happens because if block in lines 17-19 is working only if condition is true.

    Or... does it give you some errors in console?
     
  3. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    Hi Zaxvax,

    Yes "level_1_11 is available in LevelSelectState" gets printed. Basically if i click on the button on the screen the bool should fire "true" and then my Level Select State should detect the true bool and load the level. It worked with GetComponent for the first 01-10 levels. Now nothing happens...Any ideas what's wrong?
     
  4. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    if get component not working then you are searching for wrong component or searching it in wrong object or it doesn't exist on that object. Or maybe some mistakes in code that makes you think it doesn't work.
     
  5. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    I've rewriten the code thrice already, everything seems fine. I even checked the scene and it seems fine as well.

    The component is named correctly and tagged, it just doesnt get called when i click on it for some reason.
     
  6. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Well it always helps to make a clean scene and try to build this small part of your project from scratch. Takes 15 - 30 minutes but helps to isolate everything from other scripts and test your code and scene.
    And also small part of project like that you can always post here and ask ppl help with it if still have problems.
     
  7. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    Is the issue that level_1_11 is a GameObject not a MouseButton_Level_1_11?



    Code (csharp):
    1. Debug.Log ("level_1_11 is available in LevelSelectState");
    2. MouseButton_Level_1_11 alternateLevel1_11 = (level_1_11 as MouseButton_Level_1_11);
    3.                         if(alternateLevel1_11.levelOneDashElevenGameButton) // Cannot get this reference from my MouseButton Script!!!
    4.                         {
    5.                             Debug.Log("You have selected Level 1_11");
    6.                             Time.timeScale = 1;
    7.                             manager.SwitchState (new PlayStateScene_11 (manager));
    8.                         }
     
  8. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    MouseButton_Level_1_11 alternateLevel1_11 = (level_1_11 as MouseButton_Level_1_11);

    must look like

    MouseButton_Level_1_11 alternateLevel1_11 = level_1_11.GetComponent<MouseButton_Level_1_11>();
     
  9. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    Hi rrh,

    Thanx for the reply, been caught up with work over the last few days hence my absence. Level1_11 is how i store my reference to the 'MouseButton_Level_1_11' script which i need to call when i press the actual GUITexture on the screen.
    The issue is not the fact that Level_1_11 is a GameObject, rather that if i press the button on the screen for some reason it doesnt "inform" my Level Select State that the button has been pressed in which case i cannot load the selected level.

    Strange as i have been doing the exact same thing and it work fine for all my other buttons.
     
    Last edited: Jul 7, 2014
  10. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    Hi Zaxvax,

    Thanx for the reply. I tried your suggestion above and i get a error due to the syntax.

    This problem is making me nuts, even today i tried rewriting all the code related with my issue and i still didn't get anything working. The two scripts (MouseButton_Level_1_11 and LevelSelectState) are just not communicating with each other. I made an additional two levels again and i can select both just fine on my LevelSelect Screen using the exact same script setup as before.

    Do you think it might be a bug???
     
    Last edited: Jul 7, 2014
  11. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    Oh I got confused between level1_11Obj and level1_11.

    Are you using AddComponent where you should be using GetComponent?

    Because if you add the MouseButton_Level_1_11 and then immediately check the value of the bool, it's going to be the default value that it sets, won't it?

    Or are you unclear on when this code gets executed? This code is going to check if levelOneDashElevenGameButton is true RIGHT NOW when the code is executed, it's not necessarily going to come back here if levelOneDashElevenGameButton later becomes true.