AssertObjectExistenceOnEnable.cs 977 B

1234567891011121314151617181920212223242526272829
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// When enabled, checks if its prefab has been instantiated and still exists. If not, makes another.
  6. /// Used in the ArUco Drone Wars sample so that destroyed drones are replaced, but only after its marker goes off-screen.
  7. /// </summary>
  8. public class AssertObjectExistenceOnEnable : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// Prefab to respawn if it doesn't exist in OnEnable().
  12. /// </summary>
  13. [Tooltip("Prefab to respawn if it doesn't exist in OnEnable().")]
  14. public GameObject prefab;
  15. [SerializeField]
  16. private GameObject instantiatedPrefab;
  17. private void OnEnable()
  18. {
  19. if(!instantiatedPrefab || instantiatedPrefab.Equals(null))
  20. {
  21. instantiatedPrefab = Instantiate(prefab, transform.parent, false);
  22. instantiatedPrefab.transform.localScale = transform.localScale;
  23. }
  24. }
  25. }