- Home /
enable/disable private gameObject
I'm having an issue with enabling an object in my scene, the problem is I use a tag to find what the object is but when I disable the object and need to reenable it again, the line of code that finds the objects tag cannot find the tag as it has been disabled, which returns an error.
I can make it a public variable and get it to work that way but I have six objects with this script on it, and would not like to assign the object in the Inspector for each one. Here is my code:
private GameObject fireTerrain;
if(gameObject.tag == "fire")
{
gameObject.renderer.material.color = Color.red;
fireField = Instantiate(Resources.Load("Lava")) as GameObject;
fireField.transform.parent = battleArena.transform;
fireField.transform.position = battleArena.transform.position + new Vector3(0.0f, 0.1f, 12.0f);
fireTerrain = GameObject.FindGameObjectWithTag("fireTerrain"); //-< finds the object first but can't find it when tis disabled
fireTerrain.SetActive(true); //trying to reactive the object when i collide with the trigger
StartCoroutine(raiseVolcano());
}
IEnumerator lowerVolcano()
{
Debug.Log("lower");
while (fireTerrain.transform.position.y >= -5)
{
fireTerrain.transform.position -= new Vector3(0.0f, 0.1f, 0.0f);
yield return null;
if (fireTerrain.transform.position.y <= -5)
{
fireTerrain.SetActive(false); //Disables the object
}
}
}
Is there a way I can reactive a disabled object if it is private?
Answer by adsamcik · Jan 17, 2015 at 07:22 PM
I had this kind of problem too and solved it this way. I loaded up the objects into the array/variables and then disabled them. (if they are present at the scene all the time). This way you already have them loaded in array and can easily and without problem look for the one you want to enable
@adsamcik You're answer made me realise my error, I was trying to find the object during the onCollisionEnter
, I have now put the FindGameObjectWithTag
line of code in the Start()
function and everything works now, many thanks.