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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Toggle Game Objects On/Off Script

Discussion in 'Scripting' started by Ozbux, May 2, 2020.

  1. Ozbux

    Ozbux

    Joined:
    May 2, 2020
    Posts:
    2
    Hi there.
    So I'm tyring to make a 2D Platformer where you can toggle some platforms on and off. I've created a sample scene with two platforms, one coloured black under the tag 'Black' and one coloured white under the tag 'White'. I'm hoping to create a script where once you press the 'Q' button on the keyboard, the white platform turns off and the black palform stays on. Then once the 'Q' button is pressed again, the white platform turns back on and the black platform turns off.

    My coding knowledge is beyond poor but any and all help I could get would be amazing thanks. Toggle example scene.PNG
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You could simple toggle a boolean and set each platform active based on its value.

    Code (CSharp):
    1.         private bool _togglePlatforms;
    2.  
    3.         //Populate these in the inspector
    4.         [SerializeField]
    5.         private GameObject _whitePlatform, _blackPlatform;
    6.  
    7.         private void Update()
    8.         {
    9.             if(Input.GetKeyDown(KeyCode.Q))
    10.             {
    11.                 //Swithces the boolean from true to false each button press;
    12.                 _togglePlatforms = !_togglePlatforms;
    13.  
    14.                 //Enable the white platform when the boolean is false, disable when true
    15.                 _whitePlatform.SetActive(!_togglePlatforms);
    16.                 //Enable the black platform when the boolean is true, disable when false
    17.                 _blackPlatform.SetActive(_togglePlatforms);
    18.             }
    19.         }
     
    Ozbux likes this.
  3. Ozbux

    Ozbux

    Joined:
    May 2, 2020
    Posts:
    2
    Thank you so so so so much for this!!!!
     
  4. benjaminbutcherhimself

    benjaminbutcherhimself

    Joined:
    Jun 18, 2021
    Posts:
    1
    Hi! Coding baby here.
    With this code, I understnad (I think) that monobehaviour allows me to reference different objects, but I guess what I'm wondering is: How do I make this script apply to the object I want to toggle on and off? Do I just add as a component to the object? Do I need to specifically refer to it in the script? If so, how?

    Obviously I don't expect a reply, but uh yeah... it'd be insanely good of you haha.

    thx

     
  5. FaffyWaffles

    FaffyWaffles

    Joined:
    Apr 10, 2020
    Posts:
    45
    You should just be able to assign the objects in the inspector once added to a object