Search Unity

(SOLVED)Button press to toggle position

Discussion in 'Scripting' started by LadyLegend, May 23, 2020.

  1. LadyLegend

    LadyLegend

    Joined:
    Oct 6, 2017
    Posts:
    50
    Why won't this work???

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PerspectiveSwitch : MonoBehaviour
    6. {
    7.     public Transform perspective, firstPerson, thirdPerson;
    8.     private bool swap;
    9.  
    10.  
    11.     void Update()
    12.     {
    13.         if (OVRInput.GetDown(OVRInput.Button.PrimaryThumbstick))
    14.         {
    15.             if (swap == false)
    16.             {
    17.                 perspective.transform.position = thirdPerson.transform.position;
    18.                 swap = true;
    19.             }
    20.             else
    21.                 perspective.transform.position = firstPerson.transform.position;
    22.                 swap = false;
    23.         }
    24.     }
    25.  
    26. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You're missing curly braces for your "else" block so only the first statement is part of it.
     
    leftshoe18 likes this.
  3. LadyLegend

    LadyLegend

    Joined:
    Oct 6, 2017
    Posts:
    50
    This Worked though

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PerspectiveSwitch : MonoBehaviour
    6. {
    7.     public GameObject perspective;
    8.     private bool swap;
    9.  
    10.  
    11.     void Update()
    12.     {
    13.         if (OVRInput.GetDown(OVRInput.Button.PrimaryThumbstick))
    14.         {
    15.             if (swap == false)
    16.             {
    17.                 perspective.SetActive(true);
    18.                 swap = true;
    19.             }
    20.             else
    21.             {
    22.                 perspective.SetActive(false);
    23.                 swap = false;
    24.             }
    25.              
    26.         }
    27.     }
    28.  
    29. }


    Is the brackets in the else statement really what made it work? Or because I changed the script to toggle a bool on and off instead of changing positions. I feel like I did have those curly brackets before when I was trying to change positions but it wasn't working either. Well if I can recreate it the problem I'll post it here if not it was solved!
     
  4. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    The brackets are important. If you put the brackets after a conditional statement (if/while/else/etc...) then it will run everything in the brackets if the condition is met. If you don't have the brackets then it will only run the first line of code after the conditional and will always run everything below it. So in your first example it was always setting swap back to false at the end of the function.

    I basically put brackets after a conditional statement every single time so if I ever need to add another line or change up what happens when the condition is met I don't need to screw around with formatting at that point.
     
  5. LadyLegend

    LadyLegend

    Joined:
    Oct 6, 2017
    Posts:
    50
    Yeah I couldn't recreate it so I must of left it out before thanks guys!