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

Code Isn't Working

Discussion in 'Scripting' started by birddd, Aug 27, 2014.

  1. birddd

    birddd

    Joined:
    Aug 27, 2014
    Posts:
    2
    I have code that's supposed to rotate a square, but it's not working, with no errors in console.

    Code (JavaScript):
    1. var CamDefault : GameObject;
    2. var CamR : GameObject;
    3. var CamUpside : GameObject;
    4. var CamL : GameObject;
    5. var CamDefaultIsActive : boolean = false;
    6. var CamRIsActive : boolean = false;
    7. var CamLIsActive : boolean = false;
    8. var CamUpsideIsActive : boolean = false;
    9. function Start()
    10. {
    11.     CamDefaultIsActive = true;
    12.     CamDefault.SetActiveRecursively (true);
    13. }
    14. function Update()
    15. {
    16.    if(Input.GetKeyDown(KeyCode.Space))
    17.    {
    18.         if(CamDefaultIsActive == true)
    19.         {
    20.             CamDefaultIsActive = false;
    21.             CamRIsActive = true;
    22.             CamDefault.SetActiveRecursively (false);
    23.             CamR.SetActiveRecursively (true);
    24.         }
    25.         if(CamRIsActive == true)
    26.         {
    27.             CamRIsActive = false;
    28.             CamUpsideIsActive = true;
    29.             CamR.SetActiveRecursively (false);
    30.             CamUpside.SetActiveRecursively (true);
    31.         }
    32.         if(CamUpsideIsActive == true)
    33.         {
    34.             CamUpsideIsActive = false;
    35.             CamLIsActive = true;
    36.             CamUpside.SetActiveRecursively (false);
    37.             CamL.SetActiveRecursively (true);
    38.         }
    39.         if(CamLIsActive == true)
    40.         {
    41.             CamLIsActive = false;
    42.             CamDefaultIsActive = true;
    43.             CamL.SetActiveRecursively (false);
    44.             CamDefault.SetActiveRecursively (true);
    45.         }
    46.    }
    47. }
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    What variable represents the square in this code? Also, where are you trying to rotate anything?
     
  3. birddd

    birddd

    Joined:
    Aug 27, 2014
    Posts:
    2
    The cameras them self are rotated, and I change it by enabling/disabling them. The square itself has nothing to do in the code, and the camera switching gives the illusion of the square rotating.
     
  4. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Your if statements fall through into each other. Block one sets the conditions for block 2 to be true, which in turn sets block 3, which in turn sets block 4, which will finally reset conditions for block one. You literally will run through all 4 blocks in a single frame/button press. I'm guessing you want the logic of "else if".

    SetActiveRecursively is also a deprecated/obsolete function as of 4.0, so i'm not sure what problems that might cause.
     
    toreau likes this.