How to delete individual instances of a prefab?

So I have several objects in my game that I have scripted to disappear after about 10 seconds when the player enters them. My only problem is that the very first instance of the objects disappears, and for good reason because I set the first instance to be the object to be deleted in the inspector. I’m well aware I need to use arrays, but then I need to alter my script in ways that I’m not very sure about (I’m not the best with arrays, for I just have a basic knowledge of them). So, I’m hoping you guys could help me with this!

Here’s my scripts:

Collision Script (refers to destruction script after collision):

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class PlayerActivate : MonoBehaviour {
	public LightScriptTrue LightScript;
	public GameObject SpotOfLight;

	// Use this for initialization
	void Awake () {
		LightScript = SpotOfLight.GetComponent<LightScriptTrue> ();
	}
	void OnTriggerEnter(Collider other)
	{    
		//Light Collision
		if (other.gameObject.CompareTag ("Light")) {
			LightScript.destrucInitiate = true;
		
		}
	}
}

Here is my destruction script:

using UnityEngine;
using System.Collections;

public class LightScriptTrue : MonoBehaviour {

	public bool destrucInitiate;
	public float destrucTimer = 10;

	// Use this for initialization
	void Start () {
		destrucInitiate = false;
	}
	
	// Update is called once per frame
	void Update () {

		if (destrucInitiate == true) {
			destrucTimer -= Time.deltaTime;
		}
		if (destrucTimer <= 0) {
			Destroy (gameObject);
		}
	}
}

I think I understand what you are missing.

This is the base you need to understand : OnTriggerEnter(Collider other) is based on the compoenent BoxCollider/SphereCollider/[Any]Collider provided by Unity.

So in your case, the sphere is the object that has the collider and enters the method. So, the sphere is the only object that is supposed to have the tag “Light” => You are using this tag to find if your object is the type of object you want in the collision method, but the object that collides is just the sphere part, not your spot light and not the parent gameobject that contains the spot and the sphere.

Next, which object has the script LightScriptTrue ? Is it the sphere ? Or it is the container object SpotOfLight that contains the sphere and the spot light ? => Only one object is suppose to have the script, and if it’s the parent, what you need to do is ;

void OnTriggerExit (Collider other)
{
         if (other.gameObject.CompareTag ("Light")) {
                  Transform parentOther = other.transform.parent;
                  parentOther.gameObject.GetComponent<LightScriptTrue>().destrucInitiate = false;
         } 
}

Ok so I took another look at it and found your problem. You’ll probably hit your head after I tell you :stuck_out_tongue:

So first all, your LightScriptTrue class is not to blame. However, you have a public variable in your PlayerActivate class that is to blame. Essentially, you are setting the LightScriptTrue script to be equal to a certain component. Hence this code:

void Awake () {
         LightScript = SpotOfLight.GetComponent<LightScriptTrue> ();
     }

And then, when your PlayerActivate 2D collidor triggers, you have this

   if (other.gameObject.CompareTag ("Light")) {
             LightScript.destrucInitiate = true;
         }

So what you’re doing is that you’re always grabbing that once instance and always setting that one instance. That is why your other LightScripts don’t trigger. What you want is this:

       if (other.gameObject.CompareTag ("Light")) {
                 other.gameObject.GetComponent<LightScriptTrue>().destrucInitiate = true;
             }

As a test, I bet Unity gives you a warning or error that LightScript doesn’t exist or have been destroyed when you try to activate another light after the first is gone.

That should work :slight_smile: