- Home /
The question is answered, right answer was accepted
Problem activating an object
I'm having an issue re-enabling a gameObject, I have the following code
using UnityEngine;
using System.Collections;
public class tileColorChange : MonoBehaviour {
private GameObject fireField;
private GameObject battleArena;
private GameObject fireTerrain;
void Start()
{
battleArena = GameObject.FindGameObjectWithTag("battleArena");
//Find the fireTerrain GameObject
fireTerrain = GameObject.FindGameObjectWithTag("fireTerrain");
//Returns the fireTerrain gameObject as expected
Debug.Log(fireTerrain)
//Disable the gameObject, this works fine
fireTerrain.SetActive(false);
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "rusty" || col.gameObject.tag == "wheels" || col.gameObject.tag == "bulbus")
{
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);
//Shows as NULL
Debug.Log(fireTerrain);
//This fails as a result
fireTerrain.SetActive(true);
StartCoroutine(raise());
}
}
}
void OnTriggerExit()
{
gameObject.renderer.material.color = Color.black;
Destroy(fireField);
StartCoroutine(lower());
}
IEnumerator raise()
{
if (gameObject.tag == "fire")
{
while (fireTerrain.transform.position.y <= 0)
{
fireTerrain.transform.position += new Vector3(0.0f, 0.1f, 0.0f);
yield return null;
}
}
}
IEnumerator lower()
{
if (gameObject.tag == "fire")
{
while (fireTerrain.transform.position.y >= -13)
{
fireTerrain.transform.position -= new Vector3(0.0f, 0.1f, 0.0f);
yield return null;
if (fireTerrain.transform.position.y <= -13)
{
fireTerrain.SetActive(false);
}
}
}
}
}
I have commented where I am having an issue but basically, I disable a gameObject in the start function, I then want to re-enable it when I stand on another tile gameObject I have in the scene.
In the Start function, the gameObject is set to fireTerrain, which is correct. But during the OnTriggerEnter function, when I debug the object again, it shows as NULL this time.
If anyone could show me where I'm going wrong, I'd appreciate it
I'm not sure what caused your error, but when I adjusted your code to leave in only the bits directly related to the fireTerrain, my dummy-fireTerrain was activated & raised on trigger enter and lowered & deactivated on trigger exit. The code I tested with:
using UnityEngine;
using System.Collections;
public class tileColorChange : $$anonymous$$onoBehaviour {
private GameObject fireTerrain;
void Start()
{
fireTerrain = GameObject.FindGameObjectWithTag("fireTerrain");
fireTerrain.SetActive(false);
}
void OnTriggerEnter(Collider col)
{
fireTerrain.SetActive(true);
StartCoroutine(raise());
}
void OnTriggerExit()
{
StartCoroutine(lower());
}
IEnumerator raise()
{
while (fireTerrain.transform.position.y <= 0)
{
fireTerrain.transform.position += new Vector3(0.0f, 0.1f, 0.0f);
yield return null;
}
}
IEnumerator lower()
{
while (fireTerrain.transform.position.y >= -13)
{
fireTerrain.transform.position -= new Vector3(0.0f, 0.1f, 0.0f);
yield return null;
if (fireTerrain.transform.position.y <= -13)
{
fireTerrain.SetActive(false);
}
}
}
}
Thanks, I tried emulating your method but had no results unfortunately, I managed to get it working though using if statements in the start function, thanks for the help :)
Answer by JayFitz91 · Feb 15, 2015 at 02:07 PM
Managed to get it working, using if statements in the start function to identify the correct tile, everything works accordinly now, thanks for the help :)
using UnityEngine;
using System.Collections;
public class tileColorChange : MonoBehaviour {
private GameObject fireField;
private GameObject battleArena;
private GameObject fireTerrain;
void Start()
{
battleArena = GameObject.FindGameObjectWithTag("battleArena");
if (gameObject.tag == "fire")
{
fireTerrain = GameObject.FindGameObjectWithTag("fireTerrain");
fireTerrain.SetActive(false);
}
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "rusty" || col.gameObject.tag == "wheels" || col.gameObject.tag == "bulbus")
{
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);
//Shows as fireTerrain GameObject now
Debug.Log(fireTerrain);
//Everything works as normal
fireTerrain.SetActive(true);
StartCoroutine(raise());
}
}
}
void OnTriggerExit()
{
gameObject.renderer.material.color = Color.black;
Destroy(fireField);
StartCoroutine(lower());
}
IEnumerator raise()
{
if (gameObject.tag == "fire")
{
while (fireTerrain.transform.position.y <= 0)
{
fireTerrain.transform.position += new Vector3(0.0f, 0.1f, 0.0f);
yield return null;
}
}
}
IEnumerator lower()
{
if (gameObject.tag == "fire")
{
while (fireTerrain.transform.position.y >= -13)
{
fireTerrain.transform.position -= new Vector3(0.0f, 0.1f, 0.0f);
yield return null;
if (fireTerrain.transform.position.y <= -13)
{
fireTerrain.SetActive(false);
}
}
}
}
}
Answer by awplays49 · Feb 15, 2015 at 01:57 PM
I had a similar issue. If its not activating the second time, It could mean some of the activation code crucial to its activation is in the deactivated object. Remember that when an object is deactivated, Their scripts dont work.
Also change
private GameObject fireTerrain;
to
public Terrain fireTerrain;
and drag it in. Then delete the part where you "find" it. Do this for all of the ones you "found", because GameObject.Find is extremely SLOW. Use public variables to reference objects and things like so.
Hi, thanks for the input, but my fireTerrain is not actually a terrain but a gameObject I made in 3ds max that emulates a lava field, I should probably name it better :P
The issue with using public is I have 6 objects with this script and 6 tiles, which means I would have to drag 6 objects onto each tile, resulting in 36 different objects, I find it would be easier to do it with Find, as it only happens in the Start function, it shouldn't be too slow
Yea good idea. Im almost positive your gameobject, though, is just deactivated and cant respond. is there any part of it that plays a role in its activation? what youre describing sounds like it has something on it that needs to be used to deactivate.
I'm not too sure what caused it but, I set it up to check for its tag in the if statement so it loads it there, I then deactivate it immediately after and it seems to work. I have more than fireTerrain, ive other Terrains too, I just used one for this question, I'm guessing it must have been trying to find all 6, either way, its running now and everything seems ok.
Thanks again for the help :)