Advertisement
Guest User

Simple check condition, do something script

a guest
Sep 10th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. // @kurtdekkker
  4. //
  5. // A generic way to set up a checker in Unity3D that will live
  6. // on another GameObject and periodically check a condition.
  7. //
  8. // When that condition returns true, this does an action and
  9. // destroys itself.
  10. //
  11. // A prime use of this is to reactivate an inactive GameObject.
  12. // Inactive GameObjects will not have scripts run, so this must
  13. // live on another GameObject entirely, and the Create() method
  14. // takes care of this.
  15. //
  16. // Example use, to resurrect a fallen enemy:
  17. //
  18. //  // when you kill the enemy:
  19. //  EnemyGameObject.SetActive(false);
  20. //  // to make the enemy reappear:
  21. //  CheckConditionAndPerformAction.Create(
  22. //      () => { return TimeToResurrectThisEnemyYet( EnemyGameObject); },
  23. //      () => { EnemyGameObject.SetActive(true); }
  24. //  );
  25. //  // in the above example you must provide a TimeToResurrectThisEnemyYet(GameObject)
  26. //  // method that knows when this enemy is due to reappear.
  27. //
  28. // Example use, to give a one-time extra life when you reach 1000 points:
  29. //
  30. //  CheckConditionAndPerformAction.Create(
  31. //      () => { return UserScore >= 1000; },    // condition to check
  32. //      () => { PlayerLifeCounter++; },         // what we do when condition is true
  33. //      0.1f);                                  // how often we check (seconds interval)
  34.  
  35. public class CheckConditionAndPerformAction : MonoBehaviour
  36. {
  37.     System.Func<bool> ConditionToCheck;
  38.     System.Action ActionToPerform;
  39.     float CheckingInterval;
  40.  
  41.     public static CheckConditionAndPerformAction Create(
  42.         System.Func<bool> ConditionToCheck,
  43.         System.Action ActionToPerform,
  44.         float CheckingInterval = 0)
  45.     {
  46.         var go = new GameObject("CheckConditionAndPerformAction.Create();");
  47.  
  48.         var checker = go.AddComponent<CheckConditionAndPerformAction>();
  49.  
  50.         checker.ConditionToCheck = ConditionToCheck;
  51.         checker.ActionToPerform = ActionToPerform;
  52.         checker.CheckingInterval = CheckingInterval;
  53.  
  54.         return checker;
  55.     }
  56.  
  57.     float age;
  58.  
  59.     private void Update()
  60.     {
  61.         age += Time.deltaTime;
  62.         if (age >= CheckingInterval)
  63.         {
  64.             age -= CheckingInterval;
  65.  
  66.             if (ConditionToCheck())
  67.             {
  68.                 ActionToPerform();
  69.  
  70.                 Destroy(gameObject);
  71.             }
  72.         }
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement