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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GameObject.SetActive () is not setting some objects to active.

Discussion in 'Scripting' started by EliteWalrus, Jul 15, 2015.

  1. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    I have a script that contains all the methods for button presses as well as a Pause function that is called when escape is pressed from another script. I had it working fine last night, but then I tweaked it a bit, added a slider and now it's giving me trouble. The crosshair image is set inactive as it should and Time.timeScale = 0 works fine but the buttons (Resume,Options, ExitToMainMenu) do not get set as active. I'm not receiving Null reference exceptions and I've used Debug.Log () to ensure the GameObject variables are being assigned properly.

    Here is the entire Script:
    Public Static Void Pause () is the method I need help with:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class Buttons : MonoBehaviour {
    6.  
    7.     static GameObject player;
    8.     static GameObject canvas;
    9.     static GameObject play;
    10.     static GameObject options;
    11.     static GameObject exit;
    12.     static GameObject map;
    13.     static GameObject jungle;
    14.     static GameObject desert;
    15.     static GameObject tundra;
    16.     static GameObject swamp;
    17.     static GameObject crosshair;
    18.     static GameObject resume;
    19.     static GameObject exitToMainMenu;
    20.     static GameObject grassDensity;
    21.  
    22.     void Start () {
    23.         canvas = GameObject.Find ("Canvas");
    24.  
    25.         if (Application.loadedLevel == 0) {
    26.             MainMenuButtons ();
    27.         } else if (Application.loadedLevel == 1) {
    28.             SwampButtons ();
    29.         }
    30.     }
    31.  
    32.     void MainMenuButtons () {
    33.         play = canvas.transform.FindChild ("Play").gameObject;
    34.         options = canvas.transform.FindChild ("Options").gameObject;
    35.         exit = canvas.transform.FindChild ("Exit").gameObject;
    36.         map = canvas.transform.FindChild ("Map").gameObject;
    37.         jungle = canvas.transform.FindChild ("Jungle").gameObject;
    38.         desert = canvas.transform.FindChild ("Desert").gameObject;
    39.         tundra = canvas.transform.FindChild ("Tundra").gameObject;
    40.         swamp = canvas.transform.FindChild ("Swamp").gameObject;
    41.     }
    42.  
    43.     void SwampButtons () {
    44.         crosshair = canvas.transform.FindChild ("Crosshair").gameObject;
    45.         resume = canvas.transform.FindChild ("Resume").gameObject;
    46.         options = canvas.transform.FindChild ("Options").gameObject;
    47.         exitToMainMenu = canvas.transform.FindChild ("ExitToMainMenu").gameObject;
    48.         resume.gameObject.SetActive (false);
    49.         options.gameObject.SetActive (false);
    50.         exitToMainMenu.gameObject.SetActive (false);
    51.     }
    52.  
    53.     public void Play () {
    54.         play.SetActive (false);
    55.         options.gameObject.SetActive (false);
    56.         exit.gameObject.SetActive (false);
    57.         map.gameObject.SetActive (true);
    58.         jungle.gameObject.SetActive (true);
    59.         desert.gameObject.SetActive (true);
    60.         tundra.gameObject.SetActive (true);
    61.         swamp.gameObject.SetActive (true);
    62.     }
    63.  
    64.     public void Swamp () {
    65.         Application.LoadLevel ("Swamp");
    66.     }
    67.  
    68.     public static void Pause () {
    69.         crosshair.gameObject.SetActive (false);
    70.         resume.gameObject.SetActive (true);
    71.         options.gameObject.SetActive (true);
    72.         exitToMainMenu.gameObject.SetActive (true);
    73.         Time.timeScale = 0;
    74.     }
    75.  
    76.     public void Resume () {
    77.  
    78.     }
    79.  
    80.     public void Options () {
    81.  
    82.     }
    83.  
    84.     public void GrassDensity (float sliderValue) {
    85.         Terrain.activeTerrain.detailObjectDensity = sliderValue;
    86.     }
    87. }
    88.  
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    im not seeing any setactive on grassdensity
     
  3. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    I haven't added that yet. That still doesn't explain why the 3 other buttons are not becoming active.
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Is there a reason you have them as static?
     
  5. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    They are static because otherwise If I wanted to call it from the player script I would actually need and instance and since the script is on all button game objects it might get a little confusing.
    Example:
    Buttons.Pause (); // Seems better than...
    GameObject.Find ("Canvas").transform.FindChild ("Some button").GetComponent <Buttons> ().Pause ();

    I've tried without static variables
     
  6. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Did you try to just SetActive instead of gameObject.SetActive as your statics are already GameObjects? Not sure, if it changes something, just looked weird to me.