Search Unity

Problem with GameObject Arrays

Discussion in 'Scripting' started by Svenibu, Jan 20, 2019.

  1. Svenibu

    Svenibu

    Joined:
    Jan 16, 2019
    Posts:
    1
    Good evening everyone,

    my English is not good.

    I need your help.

    I have a problem with my Script. I have 3 Game Objekts a Switch, blue barrier and a red barrier.

    When I press the switch, the blue barrier is aktiv and the red barrier not. When I press the switch again, the blue barrier is not aktiv and the red barrier is aktiv.

    I would like more barriers in my Scene. wenn i make tow GameObjects Arrays in my script. Is the Script not work.

    Switch Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RBSwitch : MonoBehaviour
    6. {
    7.  
    8.     private Animator anim;
    9.     public GameObject[] redbox;
    10.     public GameObject[] bluebox;
    11.  
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         //rotebox = GameObject.Find("/Rot/R_Box");
    18.         //blauebox = GameObject.Find("/B_Box");
    19.         //rotebox = GameObject.FindGameObjectsWithTag("switch_r");
    20.         //blauebox = GameObject.FindGameObjectsWithTag("switch_b");
    21.         anim = GetComponent<Animator>();
    22.  
    23.     }
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.  
    28.     }
    29.  
    30.     public void Smash()
    31.     {
    32.  
    33.         if (anim.GetBool("rbonoff") == true)
    34.         {
    35.  
    36.             anim.SetBool("rbonoff", false);
    37.             redbox.GetComponent<rswitch>().switchred();
    38.             bluebox.GetComponent<bswitch>().switchblue();
    39.         }
    40.         else
    41.         {
    42.             anim.SetBool("rbonoff", true);
    43.             bluebox.GetComponent<bswitch>().switchblue();
    44.             redbox.GetComponent<rswitch>().switchred();
    45.  
    46.         }
    47.  
    48.  
    49.     }
    50. }
    Red Barrier
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class rswitch : MonoBehaviour
    6. {
    7.     private Animator anim;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         anim = GetComponent<Animator>();
    13.     }
    14.  
    15.         public void switchred()
    16.     {
    17.         if (anim.GetBool("rswitch") == true)
    18.         {
    19.             anim.SetBool("rswitch", false);
    20.         }
    21.         else
    22.         {
    23.             anim.SetBool("rswitch", true);
    24.  
    25.         }
    26.  
    27.     }
    28. }
    29.  
    Blue Barrier
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class bswitch : MonoBehaviour
    6. {
    7.     private Animator anim;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         anim = GetComponent<Animator>();
    13.     }
    14.  
    15.     public void switchblue()
    16.     {
    17.         if (anim.GetBool("bshwitch") == true)
    18.         {
    19.             anim.SetBool("bshwitch", false);
    20.         }
    21.         else
    22.         {
    23.             anim.SetBool("bshwitch", true);
    24.  
    25.         }
    26.  
    27.     }
    28. }
    best regards
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Hi