Search Unity

The single most useful simple script ever (please adjust to make it better)

Discussion in 'General Discussion' started by realghetto, Jun 21, 2014.

  1. realghetto

    realghetto

    Joined:
    Jan 21, 2014
    Posts:
    112
    http://keysofnine.com/the-most-useful-generic-unity-3d-script-ever/
    this component is addeed to 50% of my game objects.

    using UnityEngine;
    using System.Collections;

    Code (csharp):
    1.  
    2. [System.Serializable]
    3. public class ObjectsToActivate
    4. {
    5. public GameObject gameObject;
    6. public bool activateAfterTime = false;
    7. public float timeToWaitBeforeActivating = 1f;
    8. public bool activateAfterTimeRandom = false;
    9. public float minTime = 1f;
    10. public float maxTime = 2f;
    11. public bool enableObject;
    12. public bool enableTrigger = false;
    13. public bool disableTrigger = false;
    14. public bool disableObject;
    15. public bool destroyObject;
    16. //public bool disableTriggerAfterUse = true;
    17. }
    18.  
    19. [AddComponentMenu("Lurk/Enable or Disable an Object")]
    20. public class EnableDisableObject : MonoBehaviour
    21. {
    22. public bool usePlayersTrigger = true;
    23. public bool useCustomTrigger = false;
    24. public string customTriggerName = "custom";
    25. public bool randomlyChooseObject = false;
    26. public bool disableThisTriggerOnActivate = false;
    27. public bool doOnStart;
    28. public bool doOnEnable;
    29. public bool doAfterTimeReapeatable;
    30. public ObjectsToActivate[] objectsToMod;
    31.  
    32. void OnEnable()
    33. {
    34. if (doAfterTimeReapeatable)
    35. {
    36. for (int i = 0; i <= objectsToMod.Length - 1; i++)
    37. {
    38. if (objectsToMod .activateAfterTime)
    39. {
    40. StartCoroutine (WaitForTimeBeforeActivating (i));
    41. }
    42. }
    43. }
    44. if (doOnEnable)
    45. {
    46.  
    47. DoTheWork();
    48. }
    49. }
    50.  
    51. void Start()
    52. {
    53. if (doOnStart)
    54. {
    55. DoTheWork();
    56. }
    57. }
    58.  
    59. void DoTheWork()
    60. {
    61. //Debug.Log ("Activated "+ gameObject.name);
    62. if (randomlyChooseObject)
    63. {
    64. for (int i = 0; i < objectsToMod.Length; i++)
    65. {
    66. ObjectsToActivate obj = objectsToMod;
    67. if (obj.gameObject != null)
    68. {
    69. if (obj.enableObject)
    70. {
    71. obj.gameObject.SetActive(false);
    72. }
    73. else if (obj.disableObject)
    74. {
    75. obj.gameObject.SetActive(true);
    76. }
    77. }
    78. }
    79.  
    80. int randItem = UnityEngine.Random.Range(0, objectsToMod.Length);
    81. if (objectsToMod[randItem].activateAfterTime)
    82. {
    83. StartCoroutine(WaitForTimeBeforeActivating(randItem));
    84. }
    85. else if (objectsToMod[randItem].activateAfterTimeRandom)
    86. {
    87. StartCoroutine(WaitForTimeBeforeActivatingRandom(randItem));
    88. }
    89. else
    90. {
    91. ModifyObject(objectsToMod[randItem]);
    92. }
    93. }
    94. else
    95. {
    96. for (int i = 0; i < objectsToMod.Length; i++)
    97. {
    98. //Debug.Log(objectsToMod.gameObject.name.ToString());
    99. if (objectsToMod.activateAfterTime)
    100. {
    101. //Debug.Log("Waiting " + objectsToMod.timeToWaitBeforeActivating.ToString() + " seconds before activating " + objectsToMod.gameObject.name);
    102. StartCoroutine(WaitForTimeBeforeActivating(i));
    103. }
    104. else if (objectsToMod.activateAfterTimeRandom)
    105. {
    106. StartCoroutine(WaitForTimeBeforeActivatingRandom(i));
    107. }
    108. else
    109. {
    110. ModifyObject(objectsToMod);
    111. }
    112. }
    113. }
    114. }
    115.  
    116. void OnTriggerEnter(Collider other)
    117. {
    118. //Debug.Log("all triggers: " + other.gameObject.name);
    119. if (usePlayersTrigger) {
    120. if (other.gameObject.tag == "Player")
    121. {
    122.   DoTheWork();
    123. }
    124. }
    125. if (useCustomTrigger) {
    126. if (other.gameObject.tag == customTriggerName)
    127. {
    128. //Debug.Log("Custom triggered: " + other.gameObject.name);
    129. DoTheWork();
    130. }
    131. }
    132. if (disableThisTriggerOnActivate) Destroy(gameObject);
    133. }
    134.  
    135. public void ModifyObject(ObjectsToActivate obj)
    136. {
    137. if (obj.gameObject != null)
    138. {
    139. if (obj.enableObject)
    140. {
    141. obj.gameObject.SetActive(true);
    142. }
    143. else if (obj.disableObject)
    144. {
    145. obj.gameObject.SetActive(false);
    146. }
    147.  
    148. if (obj.disableTrigger)
    149. {
    150. //Debug.Log("Disabling trigger on " + obj.gameObject.name);
    151. obj.gameObject.collider.enabled = false;
    152. }
    153.  
    154. if (obj.enableTrigger)
    155. {
    156. //Debug.Log("Enabling trigger on " + obj.gameObject.name);
    157. obj.gameObject.collider.enabled = true;
    158. }
    159.  
    160. if (obj.destroyObject)
    161. {
    162. Destroy(obj.gameObject);
    163. }
    164. }
    165. }
    166.  
    167. IEnumerator WaitForTimeBeforeActivating(int objNumber)
    168. {
    169. yield return new WaitForSeconds(objectsToMod[objNumber].timeToWaitBeforeActivating);
    170. //Debug.Log("Now activating the object after time");
    171. ModifyObject(objectsToMod[objNumber]);
    172. }
    173.  
    174. IEnumerator WaitForTimeBeforeActivatingRandom(int objNumber)
    175. {
    176. yield return new WaitForSeconds(UnityEngine.Random.Range(objectsToMod[objNumber].minTime, objectsToMod[objNumber].maxTime));
    177. //Debug.Log("Now activating the object after time");
    178. ModifyObject(objectsToMod[objNumber]);
    179. }
    180. }
    181.  
     
    Last edited: Jul 1, 2014
  2. Whippets

    Whippets

    Joined:
    Feb 28, 2013
    Posts:
    1,775
    sheesh, format that as a code chunk. It's illegible as it is.
     
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    12,327
    I can see what it does, but I do not understand its purpose.
     
  4. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    2,667
    It's purpose it to impress script kiddies.
     
  5. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,061
    No, the most useful code would be in code blocks and the correct forum. This is just alphabet soup.
     
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,096
    The single most useful simple forum tag ever is:


    ... Code tag.
     
    carking1996 and drewradley like this.
  7. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    If it's not worth your time to explain it or properly format it, then it's definitely not worth my time to read through it and try to understand it.
     
  8. realghetto

    realghetto

    Joined:
    Jan 21, 2014
    Posts:
    112
    Seriously, i give you this and you pass it up for lack of code tags??? c'mon, i must have drank a 5th of vodka that night... seriously doubting my love to give it to you. but, we really do use this after 2 years, for TONS OF THINGS. it's generic, that is sure, but it is useful beyond measure.

    ***edit*** i added the code tags for ya biatch.
     
  9. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    6,516
    Why is this script "useful?"
     
  10. sicga123

    sicga123

    Joined:
    Jan 26, 2011
    Posts:
    780
    Hey thanks, I just copied and through it onto a game object. Nice script, definitely be useful. Thanks for sharing.
     
  11. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    It's pretty basic, entry level stuff... Nothing mighty impressive...
    PS Can you please indent the Brackets? My OCD is driving me crazy.
     
  12. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    So, how do you feel about the following snippet:
    Code (CSharp):
    1.         using UnityEngine;
    2.  
    3.         class Hell : MonoBehaviour
    4.         {
    5.     void IsOtherPeoplesCode()
    6.     {
    7. Debug.Log("Hell is other people's code");
    8.     }
    9.         }
     
  13. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    6,516
    I hate you and your entire family
     
  14. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    So does this script DoTheWork(); ?

    In which case exactly is this script useful? I can only see that it enables n gameObjects - sometimes randomly, sometimes with delay or both. Disco lights could be probably a usecase.
     
  15. pete1061

    pete1061

    Joined:
    Oct 14, 2013
    Posts:
    67
    Ditto.
     
  16. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    O_O
    *Shoots himself in the head*

    BBC News Headlines: 'Guy with a crazy OCD shoots himself in the head after being shown some hellish code format'.

    R.I.P
     
  17. sicga123

    sicga123

    Joined:
    Jan 26, 2011
    Posts:
    780
    Yes, we're all aware it's pretty basic stuff, not quite certain why it's being attacked so much, given that the guy is only trying to help someone. It's a contribution of sorts. Suddenly this thread reminds me of the garagegames forums, a more toxic environment is hard to find.
     
  18. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
    The attitude for one ...
     
  19. robhuhn

    robhuhn

    Joined:
    Jun 21, 2009
    Posts:
    113
    Actually I was serious about my question. Maybe I just can't see the great benefit because I don't know when to use it. I don't say it's not useful or useless basic stuff (a lot of basic stuff can be very useful). I'm just asking for a short description :) Also the link doesn't provide much documentation.
     
  20. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    12,327
    Attack? All we're doping is asking why he thinks it's so useful. I genuinely don't understand why I would want to use it. The OP clearly sees value in it, and I'm genuinely curious about what that is. It's not an attack, it's curiosity.

    Precisely.
     
  21. sicga123

    sicga123

    Joined:
    Jan 26, 2011
    Posts:
    780
    To quote Shakespeare, "The lady doth protest too much, methinks."
     
  22. inafield

    inafield

    Joined:
    Feb 26, 2013
    Posts:
    281
    <humor> Imho, the single most useful script ever created does the equivalent of "Hello World."
    1. It is concise.
    2. If done properly, contains no unnecessary bloat.
      1. Unless implemented in Java ;)
    3. It is applicable to nearly every programming language and platform.
    4. It is a basic sanity check of your development environment.
    5. It is friendly.
    All other "useful simple scripts" are generally specific to a particular user's need, or at least their misguided idea of what they needed. We all coded these when we first started out, and it included every recipe and ingredient under the sun even if we didn't actually need it.
     
  23. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    6,516
    That quote has absolutely no meaning in this context.
     
  24. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    7,210
    OK It looks like this code provides a delayed object/trigger/activation/destroy mechanism, with optional random time offset, which can be activation via the OnEnable() or Start() functions.

    In addition it looks like it can use OnTriggerEnter() to activate other game objects or destroy this game object.

    Then there is a ModifyObject() function that allows the object to be disabled or destroyed.

    It also have some tag dependencies requiring the tags "Player" and a customTriggerName.

    It may do more but I am attempting to read someone elses code, some comments in the code or a list of features above would be useful.
     
    inafield likes this.
  25. pete1061

    pete1061

    Joined:
    Oct 14, 2013
    Posts:
    67
    Ok, I'm not too proud to admit that I'm not as experienced as many of the other people here. I'm not a complete novice. But this script is not self explanatory to me. So, I'm politely asking the OP to please write a more detailed description as to why this is a useful script, and possibly some examples of situations it can be used.

    That's all, no attacks, just a request for more info.

    You say "it's the single most useful script ever." But nothing else.
    We're left to just figure it out ourselves.
     
    inafield likes this.
unityunity