- Home /
 
 
               Question by 
               cubelord · Dec 18, 2011 at 03:12 AM · 
                androidinstantiatetapscrip  
              
 
              instantiate object on tap
im making an android game and i want to make it so that when you tap somewhere on the screen a cube will instantiate there but im not sure how to go about it
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by dannyskim · Dec 19, 2011 at 05:25 AM
Instantiation
http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html
Touch
http://unity3d.com/support/documentation/ScriptReference/TouchPhase.html
Basic touch and Instantiation, not optimized and not suggested for practical use in it's basic state:
 using UnityEngine;
 using System.Collections;
 public class touchSpawnExample : MonoBehaviour {
     
     public GameObject itemToSpawn;
     private GameObject itemToSpawnInstance;
     
     void Update()
     {
         if( Input.touchCount == 1 )
         {
             Touch touch = Input.touches[0];
             
             switch( touch.phase )
             {
             case TouchPhase.Began:
                 itemToSpawnInstance = (GameObject)Instantiate( itemToSpawn, Vector3.zero, Quaternion.identity );
                 break;
             case TouchPhase.Moved:
                 // track touch movement and doStuff here
                 break;
             case TouchPhase.Ended:
                 // track touch lifted off screen and doStuff here
                 break;
             default:
                 break;
             }
         }
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
Unwanted multiple Taps 1 Answer
Detecting Android tap 1 Answer
Level load Time on Unity Android (Pro) 1 Answer
instantiate problem 1 Answer
How to make multiple UI buttons clickable (or in this case tapable) at once. 0 Answers