Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is this a proper way to assign a Trigger a string tag?

Discussion in 'Scripting' started by ModularMining, Oct 6, 2017.

  1. ModularMining

    ModularMining

    Joined:
    Jul 18, 2016
    Posts:
    22
    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?

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class ShowBeacon : MonoBehaviour
    9.  
    10. {
    11.     //declare the items
    12.  
    13.     //Item to spawn
    14.     public string _tag;
    15.     public GameObject Prefab;
    16.  
    17.     //The location of where to spawn.
    18.     public Transform _TransformSpot;
    19.  
    20.     //This is added to delete the spawned objects, this directory is the container.
    21.     public Transform _TransformParentDirectory;
    22.  
    23.     //How long to wait before spawning.
    24.     // examples
    25.     // 0 = immediate
    26.     // 1 = 1 second
    27.     // 2 = 2 seconds etc..
    28.     public float _DelayTime;
    29.  
    30.  
    31.     void Start()
    32.     {
    33.        
    34.        
    35.     }
    36.  
    37.     // Use this for initialization
    38.     IEnumerator OnTriggerEnter (Collider trigger)
    39.     {
    40.         if (trigger.gameObject.tag == _tag) {
    41.             yield return new WaitForSeconds (_DelayTime);
    42.             GameObject _PrefabCopy = Instantiate (Prefab, _TransformSpot.position, _TransformSpot.rotation) as GameObject;
    43.             _PrefabCopy.transform.SetParent (_TransformParentDirectory);
    44.         }
    45.            
    46.  
    47.     }
    48.        
    49.     /*void OnTriggerExit(Collider trigger)
    50.     {
    51.         //Destroy (gameObject);
    52.         int childs = transform.childCount;
    53.         for (int i = childs - 1; i >= 0; i--)
    54.         {
    55.             GameObject.Destroy (transform.GetChild (i).gameObject);
    56.         }
    57.    
    58.     }*/
    59.  
    60.  
    61. }
    62.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Can you explain why you need to create a new script for each trigger that behaves in a similar manner? That seems like a fundamental flaw in your design.
     
  3. ModularMining

    ModularMining

    Joined:
    Jul 18, 2016
    Posts:
    22
    If using Tags for OnTriggers, Exits etc.. I would need to declare the tag in script. Save, copy into new script and repeat. Using this would avoid this process and allow me to reuse the code. Based on your comment. What would you do if you need to trigger based on tags. How would you use the same code for each separate trigger?
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    This is the correct way to do it. Duplicating a script whose purpose is redundant just because you didn't expose a string as public is a terrible design path.

    You've got a full inspector for public variables, serialization in scenes, prefabs, scriptableobject asset potential, etc. There's really no reason not to make generic scripts for everything wherever possible so keep looking for ways to continue doing that. Otherwise, you'll end up with a nightmare down the line trying to update dozens of scripts that do the exact same thing.