- Home /
SetActive(true) is not working
using UnityEngine; using System.Collections;
public class Light_Manager : MonoBehaviour {
new GameObject[] light;
public float AmbientI;
// Use this for initialization
// Update is called once per frame
void Update() {
AmbientI = RenderSettings.ambientIntensity;
try
{
light = GameObject.FindGameObjectsWithTag("Light");
}
catch (System.NullReferenceException ex) {
light = GameObject.FindGameObjectsWithTag("Light");
}
if(AmbientI <= 1.1f) {
StartCoroutine(EnableBlockLight());
}
if(AmbientI > 1.1f) {
StartCoroutine(DisableBlockLight());
}
}
public IEnumerator EnableBlockLight() {
yield return new WaitForEndOfFrame();
foreach (GameObject a in light)
{
a.gameObject.SetActive(true);
}
}
public IEnumerator DisableBlockLight() {
yield return new WaitForEndOfFrame();
foreach (GameObject a in light)
{
a.gameObject.SetActive(false);
}
}
}
GamObject[] light; is prefab and instiantiate create in playing game.
my problem is if playing game, DisableBlockLight () function is working...
but EnableBlockLight() function is not working...
PS. ambientIntensity value is changing frequently...
Help to me please.. thank you..
Answer by hamado · Jun 29, 2016 at 08:09 PM
The problem might be because after disabling the game object the script looses its connection to it so it cannot activate it. This might be the issue otherwise I am not sure of any other reason.
If that is the problem however try disabling the lights instead of the gameobject completely.
a.gameObject.GetComponent().enabled = true;
Hope this helps. Good luck.
Answer by ScaniX · Jun 30, 2016 at 09:51 AM
The GameObject.Find() methods do not find inactive objects. I think there are thousands of threads about this here as pretty much everyone (including myself) at least had this problem once. :)
In addition I am wondering:
You are doing something in a try block and if it causes an exception, you are... just trying it again?
Do you really need to get all lights in the Update()? Does the list change that much? Wouldn't it be better to get them in the Awake() or Start() and only update it whenever you add/remove a light to the scene?
Why are you using Coroutines? This should not take that long. This does not seem to be a thing that should be done every few frames anyway in my humble opinion.