Since I reuse scripts alot. I was tired of creating a new script for each trigger. I am using the following; Is this a method preferred, or is there some other way? This avoids forcing me to copy, recreate, change string to the new tag. This gives me the option to type (enter) the “tag” string via public declaration in the inspector. Then added a delay to spawn and destroy with reference to a sub directory where the instantiation is placed. Otherwise it is placed at root and gets messy. I can delete the instantiated prefab from the parent declared folder. Thoughts?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShowBeacon : MonoBehaviour
{
//declare the items
//Item to spawn
public string _tag;
public GameObject Prefab;
//The location of where to spawn.
public Transform _TransformSpot;
//This is added to delete the spawned objects, this directory is the container.
public Transform _TransformParentDirectory;
//How long to wait before spawning.
// examples
// 0 = immediate
// 1 = 1 second
// 2 = 2 seconds etc..
public float _DelayTime;
void Start()
{
}
// Use this for initialization
IEnumerator OnTriggerEnter (Collider trigger)
{
if (trigger.gameObject.tag == _tag) {
yield return new WaitForSeconds (_DelayTime);
GameObject _PrefabCopy = Instantiate (Prefab, _TransformSpot.position, _TransformSpot.rotation) as GameObject;
_PrefabCopy.transform.SetParent (_TransformParentDirectory);
}
}
/*void OnTriggerExit(Collider trigger)
{
//Destroy (gameObject);
int childs = transform.childCount;
for (int i = childs - 1; i >= 0; i--)
{
GameObject.Destroy (transform.GetChild (i).gameObject);
}
}*/
}