- Home /
scripting question. turn component on/off
i want to turn a component on when i get below a certain y-coordinate, and then turn it back on when i go above said coordinate. i'm using this for an underwater effect. provided the code that im using so far. it is attached to my main camera, and the component i want to turn on/off is a projector that is also attached to my main camera. any help?
using UnityEngine;
using System.Collections;
using UnityEngine;
using System.Collections;
public class Underwater : MonoBehaviour {
public int underwaterLevel = 7;
public Color newFogColor;
public bool defaultFog = RenderSettings.fog;
public Color defaultFogColor = RenderSettings.fogColor;
public float defaultFogDensity = RenderSettings.fogDensity;
void Start () {
camera.backgroundColor = new Color(0, 0.4f, 0.7f, 1);
}
void Update () {
if (transform.position.y < underwaterLevel)
{
RenderSettings.fog = true;
RenderSettings.fogColor = (newFogColor);
RenderSettings.fogDensity = 0.04f;
}
else
{
RenderSettings.fog = defaultFog;
RenderSettings.fogColor = defaultFogColor;
RenderSettings.fogDensity = defaultFogDensity;
}
}
}
Answer by CodeMasterMike · Jan 29, 2013 at 07:50 AM
I cant show a code for some strange reason, but you can try to disable the component by doing the following steps:
Access the component you want to enable/disable by calling the GetComponent function.
Disable the component you retrieved from previous step by setting the YourComponentObject.enabled to false (or true if you want it to be active).
Good luck!