Search Unity

Better coding

Discussion in 'Scripting' started by YKurebayashi, Feb 14, 2020.

  1. YKurebayashi

    YKurebayashi

    Joined:
    Nov 25, 2019
    Posts:
    6
    Hi.
    I made a script to change the active status of two gameobjects.
    The script is pretty simple, if i press fire1 the gameobject 1 = active and gameobject 2 = deactive. If i press fire2 gameobject 1 = deactive and gameobject 2 = active.
    But I would like to make the fire1 button switch between these status.
    I tried to create a condition that says "if i press fire1 and gameobject 1 is active in hierarchy then set gameobjetive 1 = deactive and set gameobject 2 = active".
    But it didn't work out. Could someone help me?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerPower : MonoBehaviour
    6. {
    7.     public    GameObject    downsideWorld;
    8.     public    GameObject    upsideWorld;
    9.  
    10.    
    11.  
    12.  
    13.     void Update()
    14.     {
    15.         Switchworlds();
    16.     }
    17.    
    18.     public    void    Switchworlds()
    19.     {
    20.         if (Input.GetButtonDown("Fire1"))
    21.         {
    22.             downsideWorld.SetActive (false);
    23.             upsideWorld.SetActive (true);
    24.         }
    25.        
    26.         if (Input.GetButtonDown("Fire2"))
    27.         {
    28.             downsideWorld.SetActive (true);
    29.             upsideWorld.SetActive (false);
    30.         }
    31.     }
    32. }
    33.  
     
  2. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    618
    What you want is a toggle. You want to set the objects active status to what it's not - if it's true it becomes false, if it's false it becomes true.

    Try something like this:


    Code (CSharp):
    1. public class PlayerPower : MonoBehaviour
    2. {
    3.     public GameObject downsideWorld;
    4.     public GameObject upsideWorld;
    5.  
    6.     void Update()
    7.     {
    8.         Switchworlds();
    9.     }
    10.  
    11.     public void Switchworlds()
    12.     {
    13.         if (Input.GetButtonDown("Fire1"))
    14.         {
    15.             downsideWorld.SetActive(!downsideWorld.activeInHierarchy);
    16.             upsideWorld.SetActive(!upsideWorld.activeInHierarchy);
    17.         }
    18.     }
    19. }
     
    YKurebayashi and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    5,004
    Hm, your approach should work...

    You can also keep track of your own
    bool
    variable, and when you want, flip it from true to false:

    Code (csharp):
    1. myBool = !myBool;
    Then it's just a matter of using .SetActive properly:

    Code (csharp):
    1. downsideWorld.SetActive( myBool);
    2. upsideworld.SetActive( ! myBool);
    Note the exclamation mark in the second line.
     
    YKurebayashi likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    5,004
    What @Serinx posts is an excellent approach too... let me add one additional thing to it: you should have a Start() function that actually sets the correct worlds active and inactive, otherwise your script will be at the mercy of how things were at the instant the scene was saved, which could lead to some weird bugs.
     
    YKurebayashi and Serinx like this.
  5. YKurebayashi

    YKurebayashi

    Joined:
    Nov 25, 2019
    Posts:
    6
    I'd like to thank both of you!
    It worked just fine with @Serinx suggestion!
    Its great to have someone helping you when u just started!
    Thanks a lot!
     
    Kurt-Dekker likes this.
unityunity