简体   繁体   中英

Unity GameObject.SetActive doesn't not work

I wrote for my Game a Script that controls the Slots in the game by GameObject. But the script isn't able to activate or deactivate. Everything is set up as it should, I also tried another Script but that works. The "main" script doesn't.

Here is the code:

    // Update is called once per frame
    void Update()
    {
        UpdateBools();
        Crosshair();
        Slots();
    }
    void UpdateBools()
    {
        isPaused = PauseMenu.GamePaused;

    }
    public void Crosshair()
    {
        //Not important
    }
    public void Slots()
    {
        if (database.Slot1 != nothing)
        {
            Slot1.SetActive(true);
            Slot1Active = true;
        }
        else { Slot1.SetActive(false); }
        if (database.Slot2 != nothing)
        {
            Slot2.SetActive(true);
            Slot2Active = true;
        }
        else { Slot2.SetActive(false); }
        if (database.Slot3 != nothing)
        {
            Slot3.SetActive(true);
            Slot3Active = true;
        }
        else { Slot3.SetActive(false); }
        if (database.Slot4 != nothing)
        {
            Slot4.SetActive(true);
            Slot4Active = true;
        }
        else {
            Slot1.SetActive(false);
            Slot2.SetActive(false);
            Slot3.SetActive(false);
            Slot4.SetActive(false);
            Slot1Active = false;
            Slot2Active = false;
            Slot3Active = false;
            Slot4Active = false;
        }
    }
    public void SlotPic()
    {
        //I erased this because stack overflow says: mostly code, add more details but I have every detail
    }

It works like this: If one inventory slot is taken, it should be activated. For testing, I added ActivateEverything(). Also, the slot should change its picture when activated but as I said, activating the object doesn't work. Important for this task is the void Slots().

Try to setActive without if else containers. Check if your gamemode not on pause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM