using UnityEngine; using System.Collections; //Attach this to a prefab or gameobject that you would like to disable based on distance to another object. Like main camera or player. public class UnluckDistanceDisabler : MonoBehaviour { public int _distanceDisable = 1000; public Transform _distanceFrom; public bool _distanceFromMainCam; #if UNITY_4_5 [Tooltip("The amount of time in seconds between checks")] #endif public float _disableCheckInterval = 10.0f; #if UNITY_4_5 [Tooltip("The amount of time in seconds between checks")] #endif public float _enableCheckInterval = 1.0f; public bool _disableOnStart; public bool _useDistanceFade = false; public float _fadeRange = 5f; int m_fadeDistanceID = Shader.PropertyToID("_FarFadeDistance"); private int m_invFadeDistanceRangeID = Shader.PropertyToID("_InverseFarFadeRange"); Material[] m_materials; void SetupMaterials() { int matNum = 0; var meshes = transform.GetComponentsInChildren(); for (int i = 0; i < meshes.Length; i++) { matNum += meshes[i].materials.Length; } m_materials = new Material[matNum]; int cnt = 0; for (int i = 0; i < m_materials.Length; i++) { for (int j=0; j _distanceDisable * _distanceDisable){ gameObject.SetActive(false); } } public void CheckEnable(){ if (!gameObject.activeInHierarchy && (transform.position - _distanceFrom.position).sqrMagnitude < _distanceDisable * _distanceDisable){ gameObject.SetActive(true); } } }