says “For performance reasons it is recommended to not use this function every frame Instead cache the result in a member variable at startup or use GameObject.FindWithTag.”
However, when I use code like this:
function Start(){
var shopper = GameObject.Find("Shopper");
}
function OnGUI () {
var cont : CCShopper = shopper.GetComponent (CCShopper);
}
I get the error:
Assets/Scripts/Shopper/ShopperGui.js(12,32): BCE0005: Unknown identifier: ‘shopper’.
private var shopper: GameObject;
function Start(){
shopper = GameObject.Find("Shopper");
var cont : CCShopper = shopper.GetComponent (CCShopper);
}
function OnGUI() {
GuiLayout.Label(cont.whatever);
You only want to to use the find functions at script startup or when you instance a new object. Having the calls inside Update or OnGUI will not be pretty. Cache the references, then use them within your OnGUI()