Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

.SetActive(true) not working

Discussion in 'Scripting' started by NautilusCo, Sep 25, 2019.

  1. NautilusCo

    NautilusCo

    Joined:
    Jan 31, 2017
    Posts:
    5
    So, I've been searching for a solution for some time, and have found a lot of similar posts with slightly different errors, so I will get out of the way that A) I have not disabled the gameobject, or parent of the gameobject in which the script is contained, and B) I am not using .Find("Name") or something of the like to reference my gameobject - it is declared as a public variable and asigned in the inspector.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Equip : MonoBehaviour
    {
    public GameObject item;
    public GameObject holster;
    private bool itemHolstered;

    private void unHolster()
    {
    item.SetActive(true);
    holster.SetActive(false);
    itemHolstered = false;
    }

    private void Holster()
    {
    item.SetActive(false);
    holster.SetActive(true);
    itemHolstered = true;
    }
    // Start is called before the first frame update
    void Start()
    {
    Holster();
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKeyDown("x"))
    {
    if (itemHolstered == false)
    {
    unHolster();
    }
    else if (itemHolstered == true)
    {
    Holster();
    }
    }
    }
    }

    The gameobjects being assigned in the inspector:
    upload_2019-9-25_16-44-3.png

    And the gameobjects in the hierarchy - Note Equip is the gameobject to which the script is attached
    upload_2019-9-25_16-45-24.png

    Any help would be greatly apreciated as I am thoroughly stumpted.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Looks like you have your itemHolstered if/else flipped? You unholster if it's false and holster if it's true but that seems backwards to what you want.
     
  3. Vega4Life

    Vega4Life

    Joined:
    Nov 27, 2012
    Posts:
    17
  4. NautilusCo

    NautilusCo

    Joined:
    Jan 31, 2017
    Posts:
    5
    thanks, I would have been staring at that for hours before I noticed
     
  5. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You can also save some code by adding a bool to your method;

    Code (CSharp):
    1.         private void Holster(bool holstered)
    2.         {
    3.             item.SetActive(!holstered);
    4.             holster.SetActive(holstered);
    5.             itemHolstered = holstered;
    6.         }
    7.  
    8.         void Update()
    9.         {
    10.             if (Input.GetKeyDown("x"))
    11.             {
    12.                 itemHolstered = !itemHolstered;
    13.  
    14.                 Holster(itemHolstered);
    15.             }
    16.         }