Any object can be an interface object
We are not limited to the Unity UI to make interfaces in Unity, and to prove that point, we will use the ball object itself to affect the scene. While we have seen this in the past, this lesson is worth repeating:
- Select the
Ball
object and click onAdd Component
. - Select
NewScript
and typePowerSwitch
into theNewScript
dialog. - Click on
Create
and then onAdd
.
Here is the code for that script file; either type it out or copy and paste it and name the file PowerSwitch.cs:
using UnityEngine; using System.Collections; public class PowerSwitch : MonoBehaviour { public GameObject go; void OnSelect() { if(go.activeSelf) { go.SetActive(false); } else { go.SetActive(true); } } }
This code uses a single public gameobject
called go
. This gameobject
is public, which means that you will see it in the editor.
It then calls OnSelect
, which is a method called by...