Category: Unity

Unity UI: Click Me? Click You!

To prevent UI clicks from filtering down to other objects: using UnityEngine.EventSystems; if (EventSystem.current.IsPointerOverGameObject ()) return; // Check if on a UI Object, if so ignore

Unity Changing Runtime Textures

To change an object’s texture ‘runtime’ via code: public Texture theTexture; gameObject.GetComponent<Renderer> ().material.SetTexture (“_MainTex”, theTexture);

Unity Coroutines: Wait a Second!

void Whatever() { float delay = 1.0f; StartCoroutine(DoSomethingAfterWaiting(delay)); }   IEnumerator DoSomethingAfterWaiting(float delay) { yield return new WaitForSeconds(delay); // Add Code here to Do it, Do it, Do it till you’re satisfied… whatever it is yield break; }

Unity Sounds: Audio Clips

public AudioClip[] sndName; AudioSource sound = GetComponent<AudioSource>(); int sndDx = 0; float volume = .5; sound.PlayOneShot(sndName[sndDx], volume);

Unity Buttons: Disabled Color?

To ‘disable’ a Unity Button and show its disabled color, DO NOT DISABLE the button, simply set buttonName.interactable = false;

Unity Objects: To Show or Not To Show

Turn an object’s display on or off: ¬†object.GetComponent <MeshRenderer>().enabled = true or false Turn Child object’s display on or off: foreach (Transform child in object.transform) { child.gameObject.GetComponent <MeshRenderer>().enabled = true or false; }