using UnityEngine; /// /// Example of accessing different objects through references setup /// in the game object inspector. /// public class AssignExample : MonoBehaviour { #region Properties /// /// Link to a camera setup in the inspector. /// public Camera targetCamera; /// /// Link to a specific component type. /// public OtherComponent otherTarget; /// /// How often to call other component. /// public float callFrequency = 1f; #endregion #region Events /// /// Checks that all references have been assigned, outputting log /// errors if any are missing and deactivates. /// void Start() { if (this.targetCamera == null) { Debug.LogError("targetCamera is not set"); this.enabled = false; } if (this.otherTarget == null) { Debug.LogError("otherTarget is not set"); this.enabled = false; } } /// /// Points the camera at the other component and calls a method /// on OtherComponent with the desired frequency. /// void Update() { this.targetCamera.transform.LookAt(this.otherTarget.transform); if (Time.time - this.lastCall > this.callFrequency) { this.otherTarget.SetLastCalled(Time.time); this.lastCall = Time.time; } } #endregion #region Private /// /// Last time OtherComponent was called. /// private float lastCall = 0f; #endregion }