Disabling Clones? (Instantiate)

I’m using Instantiate on a game object, and I want the user to tap on the clones to disabled them without effecting the original. Is this possible?

I have the scene set up to clone an object every 10 seconds. Right now my Instantiate script to attached to an empty game object, and the “Tap” script is attached to the original game object.

The tap script is functioning, but its disabling all the clones that are on screen if I tap on the first object that was cloned.

“Tap”

var character : GameObject;


function Update () {
	
var hit : RaycastHit;

   for (var evt : iPhoneTouch in iPhoneInput.touches)   
     {
     	 if (evt.phase == iPhoneTouchPhase.Began)
     	  { 
      var ray = camera.main.ScreenPointToRay(evt.position);
      
          if(Physics.Raycast(ray, hit, 50)  hit.collider.tag == ("Blue")) {
          
       		if (iPhoneInput.touchCount == 1) { 
      			if (iPhoneInput.GetTouch(0).tapCount == 1) { 
          
     character.active = false;

“Instantiate”

var clones : GameObject;
private var Timer : float; 

function Update () {
	
      
    if ( Timer + 10 < Time.time ) {

    	Instantiate(clones, transform.position,transform.rotation); 

    	Timer = Time.time; 
    }
    
    
    
    
}

Thanks!

Brandon

I don’t know what GameObject you have “character” assigned to, but the " character.active = false; " is most likely your issue.

How else can I disable them? If I destroy them Unity throws an error.

how are you destroying them and which error you are getting?

var character : GameObject; 


function Update () { 
    
var hit : RaycastHit; 

   for (var evt : iPhoneTouch in iPhoneInput.touches)    
     { 
         if (evt.phase == iPhoneTouchPhase.Began) 
          { 
      var ray = camera.main.ScreenPointToRay(evt.position); 
      
          if(Physics.Raycast(ray, hit, 50)  hit.collider.tag == ("Blue")) { 
          
             if (iPhoneInput.touchCount == 1) { 
               if (iPhoneInput.GetTouch(0).tapCount == 1) { 
                     Destroy(character);

After the original object gets destroyed, I get a NullReferenceExeception because the prefab is now null.

well, dont destroy the original object, destroy the clone itself.

you might be wondering how?..
well store the clones in an array and destroy them accessing the references to the array :slight_smile:

Hope it helps!

Sorry, I have no idea how to create an array! :roll: (Still relatively new to programming)

rofl no worries :slight_smile:

var myArr : GameObject[];

function Start(){
    myArr = new GameObject[10]; // an array of 10 positions
}

hope it helps :smile:

Ok cool, thank you! Would I need to combine the two scripts I’m using? I have the clones being created in one script, and I’m destroying them with another.

Hello!

not necessary, you just can create a function and access one script from another with the function.

remember yourObject.GetComponent(yourScriptName).yourFunction()

is the way to access it :slight_smile: