using UnityEngine;
///
/// Example of interacting between objects using a name lookup, if
/// a reference hasn't already been assigned in the inspector.
///
public class HybridLookupExample : MonoBehaviour
{
#region Properties
///
/// Object to point camera at.
///
public Transform targetView;
///
/// Camera to point at object.
///
public Camera targetCamera;
#endregion
#region Events
///
/// Load references by name if not already assigned.
///
void Start()
{
if (this.targetView == null)
{
GameObject g = GameObject.Find("target");
if (g != null)
{
this.targetView = g.transform;
}
}
if (this.targetCamera == null)
{
GameObject g = GameObject.Find("camera");
if (g != null)
{
this.targetCamera = g.camera;
}
}
}
///
/// Keep camera pointed at object.
///
void Update()
{
// early exit for missing references
if (this.targetView == null) return;
if (this.targetCamera == null) return;
// point camera
this.targetCamera.transform.LookAt(this.targetView);
}
#endregion
}