SetActive doesn't work. How to fix this.

SomeObject stays active?
Script 1 works, Script 2 works, but Script 3 doesn’t work. It seams that script 2 keeps the object SomeObject active.
Debug.Log in Script 3 gives that it works just as it supposed to be, but the object SomeObject doesn’t set to false.
How can I set the SomeObject.SetActive to false in Script 2, witch I thing that this would be the problem.

-----Script1-----

public float interactDistants = 5f;
void Update ()
    {
        if (Input.GetKeyDown (KeyCode.O))
        {
            Ray ray = new Ray (transform.position, transform.forward);
            RaycastHit hit;
            if (Physics.Raycast (ray, out hit, interactDistants))
            {
              if (hit.collider.CompareTag ("SomeTag"))
                {
                    hit.collider.transform.GetComponent<SomeScript>().ChangeVisibleState ();
                }
             }
          }
       }

-----Script2_____

using UnityEngine;
using System.Collections;

public class SomeScript : MonoBehaviour {

    public GameObject SomeObject;

    public void ChangeVisibleState ()
    {  
        active = !active;
    }

    void Update ()
    {
        if (active)
        {
            SomeObject.SetActive (true);
        }


        if (active == false)
        { 
            SomeObject.SetActive (false);
        }




    }
}

-----Script 3-------

using UnityEngine;
using System.Collections;

public class FirehoseActivator : MonoBehaviour {
  
    public GameObject SomeObject;


    void OnTriggerEnter(Collider col)
    {
        if (col.gameObject == EmptyObject)
        {
         //Debug.Log(SomethingOutSetActiveFalse);
           SomeObject.SetActive (false);

        if (col.gameObject == EmptyObject)
        {
        //Debug.Log(SomethingOutSetActiveTrue);
            SomeObject.SetActive (true);
        }
    }

}

Script 3 has two identical IF statements. So both lines of code inside the IF statements will be executed. First you are setting your game object inactive like you want, but then you are immediately setting it back to active.

1 Like

It also looks like EmptyObject isn’t defined.

I’m sorry that I didn’t show my original script, this because I thought that it would be to confusing.

You can see it’s a mix of english and dutch, but for me it works.
What you gave me as answer is not there in my original script. The problem still is there.

Here is my original script 2.

public void ChangeVisibleState ()
    {
        active = !active;
    }

    void Update ()
    {
        if (active)
        {
            BrandhaspelGroup1.SetActive (false);
            BrandhaspelLeeg.SetActive (true);
            BrandslangKopUit.SetActive (true);
        }


        if (active == false)
        {
            BrandhaspelGroup1.SetActive (true);
            BrandhaspelLeeg.SetActive (false);
            BrandslangKopUit.SetActive (false);
        }
    }

Here is my original script 3.

void OnTriggerEnter(Collider col)
    {
        if (col.gameObject == EmptyObjectActivatorFirehose)
        {
            EmptyObjectLifes119.SetActive (false);
            BrandslangKopUit.SetActive (false);
            BrandslangKopAan.SetActive (true);
            EmptyObjectDeactivatorFirehose.SetActive (true);
            EmptyObjectActivatorFirehose.SetActive (false);
        }
        if (col.gameObject == EmptyObjectDeactivatorFirehose)
        {
            EmptyObjectLifes119.SetActive (true);
            BrandslangKopUit.SetActive (true);
            BrandslangKopAan.SetActive (false);
            EmptyObjectDeactivatorFirehose.SetActive (false);
            EmptyObjectActivatorFirehose.SetActive (true);
           
        }
    }

The object BrandhaspelKopUit stays active while it should get inactive when in script 3 the FPSControler has a collision with the object EmptyObjectActivatorFirehose. The other collision with objects does work fine.