Search Unity

Weird Variable/Button problems

Discussion in 'Scripting' started by Predster, Mar 4, 2008.

  1. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    I am having MAJOR trouble with my buttons. I thought maybe it had something to do with my GUI, but now I think it is a variable problem.

    I have the code below attached to a game object, where the button "changer" is the left ctrl, which switches camera views. However, all of my GUI buttons also cause this code to run, AND I HAVE NO IDEA WHY!! Could pressing the GUI buttons somehow be changing the variable "switcher" I am using in the code below?


    Code (csharp):
    1.  
    2. var fpsCam : GameObject;
    3. var orientationCam : GameObject;
    4. var fpsMaterial : Material;
    5. var orientationMaterial : Material;
    6. var modelUse : GameObject;
    7. var orientationLight: GameObject;
    8. var orientationLight1: GameObject;
    9. var pointLight: GameObject;
    10. private var switcher : boolean = false;
    11.  
    12. orientationLight.active = false;
    13. fpsCam.active = true;
    14. orientationCam.active = false;
    15. orientationLight1.active = false;
    16. pointLight.active = false;
    17.  
    18.  
    19.  
    20.  
    21. function Update () {
    22.    
    23.     if (Input.GetButtonDown("Changer")) {
    24.         switcher = !switcher;
    25.         if (switcher == true) {
    26.              modelUse.renderer.sharedMaterial = orientationMaterial;
    27.              fpsCam.active = false;
    28.              orientationCam.active = true;
    29.              orientationLight.active = true;
    30.              orientationLight1.active = true;
    31.              pointLight.active = true;
    32.             }
    33.         else {
    34.             modelUse.renderer.sharedMaterial = fpsMaterial;
    35.             fpsCam.active = true;
    36.             orientationCam.active = false;
    37.             orientationLight.active = false;
    38.             orientationLight1.active = false;
    39.             pointLight.active = false;
    40.             }
    41.     }
    42. }

    This is the code for my GUI buttons:
    Code (csharp):
    1.  
    2. function OnGUI()
    3. {      
    4.             // Make a background box
    5.     GUI.Box (Rect (0,Screen.height - 50,Screen.width,75), "Informative text"); 
    6.    
    7.         // Make a button.
    8.     if (GUI.Button (Rect (Screen.width - 100, Screen.height - 45,80,20), "Test")) {
    9.         print ("You clicked the button!");
    10.     }
    11. }
    12.  
    13.  
    I have been wracking my brain, trying to think of what the problem is, but I am completely stumped. Any input would be much appreciated!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Should be fine. What happens if you use something other than left ctrl? Do you have any third-party utilities that might be making a mouse button click send a left ctrl event?

    --Eric
     
  3. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    It does the same thing with any other key I assign! I don't have any third party utilities installed--in fact I've tried it on 2 different computers and it does the same thing! I'm completely stumped! :?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Maybe post the project, preferrably a cut-down version that just shows the problem? I can't see anything in the script that would do that.

    --Eric
     
  5. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    Thanks so much for the help Eric!
     
  6. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    It's the "camera switcher" script that is problematic.
     
  7. CK

    CK

    Joined:
    May 28, 2007
    Posts:
    7
    It's the "Alt Positive Button" that cause the problem, i think.

    I tried Edit->Project Setting->Input

    and delete the "mouse 0" of the "Switch" in "Alt Positive Button" and seems working.
     

    Attached Files:

  8. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    You're right!! This fixed the problem!!!!!!!! Thanks!


    :D :D :D :D :D :D