Question by
dijital · Nov 01, 2016 at 06:28 PM ·
gameobject.find
Efficiently reading variable from another script.
Hi, Im using the following code the pull a variable from another script, it seems to work fine.
But i now need to pull that variable every frame and I know using GameObject.Find is an absolute killer, how do i modify this script to make it more efficient?
Any help would be appreciated.
using UnityEngine;
using System.Collections;
public class timeregister : MonoBehaviour {
public float Alpha;
void Start()
{
Alpha = GameObject.Find("firstnumber").GetComponent<OnScreenControls>().Lion /100;
}
// Update is called once per frame
void Update () {
print (Alpha);
}
}
Comment
Best Answer
Answer by Cains · Nov 02, 2016 at 11:59 PM
You can just store the result of GameObject.Find in a variable, that way you only have to find the GameObject once and can pull the variable quickly through that reference.
public float Alpha;
private OnScreenControls controls;
private void Start()
{
controls = GameObject.Find("firstnumber").GetComponent<OnScreenControls>();
}
private void Update()
{
Alpha = controls.Lion / 100;
print(Alpha);
}