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

Resolved how to get the the number same for GetNoOfSwitch and switchesLength

Discussion in 'Scripting' started by hledward7, Mar 9, 2021.

  1. hledward7

    hledward7

    Joined:
    Jul 9, 2019
    Posts:
    8
    The problem is inside the public int GetnoOfSwitch, Using for loop isit correct?
    Code (CSharp):
    1.  
    2.     [SerializeField]
    3.     GameObject[] switches;
    4.     [SerializeField]
    5.     GameObject doorselect;
    6.  
    7.      int noOfSwitches;
    8.     // Start is called before the first frame update
    9.     private void Start()
    10.     {
    11.         GetNoOfSwitch();
    12.     }
    13.     public int GetNoOfSwitch()
    14.     {
    15.      
    16.         for (int i = 0; i < switches.Length; i++)
    17.         {
    18.            
    19.             if (switches[i].GetComponent<Switch>().isOn == false)
    20.             {
    21.                 noOfSwitches = 0;
    22.             }
    23.             else if (switches[i].GetComponent<Switch>().isOn == true)
    24.             {
    25.                 noOfSwitches =+1;
    26.             }
    27.            
    28.         }
    29.         return noOfSwitches;
    30.     }
    31.     public void GetDoorState()
    32.     {
    33.         if (GetNoOfSwitch() == switches.Length)
    34.         {
    35.             doorselect.GetComponent<Door>().Dooropen();
    36.         }
    37.         else if (GetNoOfSwitch() != switches.Length)
    38.         {
    39.             doorselect.GetComponent<Door>().Doorclose();
    40.         }
    41.     }
    42.     private void Update()
    43.     {
    44.         GetDoorState();
    45.         Debug.Log(GetNoOfSwitch());
    46.     }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    I'm guessing that line 25 is supposed to be += 1 and not =+ 1?
    += 1 increments the number by one.
    =+1 sets the number to positive one.
     
    PraetorBlue likes this.
  3. hledward7

    hledward7

    Joined:
    Jul 9, 2019
    Posts:
    8
    if the two switch is On, the number of the will increase until 2 hundred and above
     
  4. hledward7

    hledward7

    Joined:
    Jul 9, 2019
    Posts:
    8
    solved
    Code (CSharp):
    1.  public int GetNoOfSwitch()
    2.     {
    3.         int x=0;
    4.         for (int i = 0; i <switches.Length; i++)
    5.         {  
    6.             if (switches[i].GetComponent<Switch>().isOn == false)
    7.             {
    8.                 noOfSwitches =-1;
    9.                 --x;
    10.             }
    11.             else if (switches[i].GetComponent<Switch>().isOn == true)
    12.             {
    13.                 noOfSwitches =+1;
    14.                 ++x;
    15.             }
    16.         }
    17.         noOfSwitches = x;
    18.         return noOfSwitches;
    19.     }