disable gameobject component unity code example
Example 1: how to disable a gameObject unity c#
public GameObject gameObj;
gameObj.SetActive(true);
gameObj.SetActive(false);
gameObject.SetActive(true);
gameObject.SetActive(false);
Example 2: disable a component unity
using UnityEngine;
using System.Collections;
public class EnableComponents : MonoBehaviour
{
private Light myLight;
void Start ()
{
myLight = GetComponent<Light>();
}
void Update ()
{
if(Input.GetKeyUp(KeyCode.Space))
{
myLight.enabled = !myLight.enabled;
}
}
}
Example 3: how to disable all other components in gameobject in unity
foreach (MonoBehaviour component in GetComponents<MonoBehaviour>())
{
if (component != this) component.enabled = false;
}