Ilya
Ilya

Reputation: 384

method SetActive (true); does not work С# Unity

I have a game where you need to control the ball, on collision the ball disappears using the method player.SetActive (false); After that, a window with a notification about the loss should come out, I use the SetActive (true); method for all window elements, but it does not appear (all window elements are initially disabled) I tried to leave all the windows on, and then turn them off through the script at the very beginning of the game, in order to turn them on at the end of the game. Items just disappear and don't show up anymore

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

public class Player : MonoBehaviour{

    public GameObject restart;
    public GameObject label;
    public GameObject player;
    public GameObject particle;
    public GameObject particleDeath;
    public GameObject Box;
    public GameObject Menu;

    public static bool lose = false;

    void Awake(){
        lose = false;
    }

    void OnTriggerEnter2D (Collider2D other){
        if (other.gameObject.tag == "bomb")
            lose = true;
            restart.SetActive (true);          //does not work
            label.SetActive (true);            //does not work
            particleDeath.SetActive (true);    //does not work
            player.SetActive (false);
            particle.SetActive (false);
            Box.SetActive (true);              //does not work
            Menu.SetActive (true);             //does not work
    }
    
}

Upvotes: 0

Views: 2106

Answers (2)

Geppetox
Geppetox

Reputation: 1

I had the same problem and figured it out: if you call setActive(false) then the update() method of that object will not be called anymore. I tried setting setActive(false) in start() method and then setActive(true) in update() method after some condition. The update method never ran after setting setActive(false). The sollution is to use meshrenderer which does similarly (deactivates the mesh thus hiding the object):

private MeshRenderer renderer1;

void Start()
    {
        renderer1 = GetComponent<MeshRenderer>(); // Get the Mesh Renderer that is attached to this GameObject
        renderer1.enabled = false; // Disable the renderer so that it is invisisble
    }

    
    void Update()
    {

        if (DigScript.finishedDigging)
        {
            renderer1.enabled = true;
            Debug.Log("Ok now show bone  " + renderer1.enabled);

        }
        
    }

Hope this helps...

Upvotes: 0

CaptainKirby
CaptainKirby

Reputation: 159

There can be various reasons for this, but you mention that you deactivate the gameobjects at the beginning of the game running, could it be that once they are activated, that the function that deactivates them runs immediately afterwards? Thus having the appearance of them not turning on? Alternatively, do the objects have parents that are disabled? Then they will appears disabled still.

Upvotes: 1

Related Questions