Skip to content

Instantly share code, notes, and snippets.

@asus4
Created November 18, 2014 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asus4/4190d8b05816818a9bc9 to your computer and use it in GitHub Desktop.
Save asus4/4190d8b05816818a9bc9 to your computer and use it in GitHub Desktop.
Follow abstruct poroperty.
using UnityEngine;
using System;
using System.Reflection;
public class PropertyFollow : MonoBehaviour {
[Serializable]
public class TargetProp {
public string compName;
public string propName;
}
public GameObject target;
public TargetProp prop;
PropertyInfo propInfo;
Component targetComp;
Component myComp;
void Start () {
// Check component
targetComp = target.GetComponent (prop.compName);
myComp = this.GetComponent (prop.compName);
if(targetComp == null || myComp == null) {
Debug.LogWarning("No target component");
this.enabled = false;
return;
}
// Cache property
Type type = myComp.GetType ();
propInfo = type.GetProperty (prop.propName);
if(propInfo == null) {
Debug.LogWarning("No target property");
this.enabled = false;
return;
}
}
void Update () {
propInfo.SetValue (myComp, propInfo.GetValue (targetComp, null), null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment