Game Development Reference
In-Depth Information
Save the Scene by selecting File ➤ Save Scene in the top menu. Play, and when you hit the
spacebar a new sphere prefab appears.
If you keep pressing the spacebar, you are instantiating more sphere prefabs, but they each appear
at origin, overlapping each other. To keep the prefabs from piling up, use the Destroy() function to
get rid of the newly instantiated sphere after two seconds.
In the Project panel, select the Scripts folder. Then select Create ➤ Javascript. Name the new script
DestroySphere. Open DestroySphere in MonoDevelop and add Destroy(gameObject, 2f); to the
Start() function. Save the script.
Return to the editor and select the Prefabs folder in the Project panel. Select Sphere, then in the
Inspector select AddComponent ➤ Scripts and choose Destroy Sphere from the list of scripts
(Figure 4-19 ).
Figure 4-19. Adding the new Destroy Sphere script to the Sphere prefab in the Inspector
In the top menu select File ➤ Save Scene. Enter Play mode. When you press the spacebar, a
sphere prefab with a randomly generated material color will appear, then, after a two-second delay,
disappear. Perfect!
 
Search WWH ::




Custom Search