Accessing an object through a direct reference or a pointer

Hi all,

I am wondering if, as it happens in other programming languages, there’s a way in unityscript to create a reference to an object, or a “pointer” to it.

For example, given the code:
localtransform = GameObject.Find(“target”).transform

I would like to be able to address
“Gameobject.Find(“target”).transform” without the need to perform the “Find” again, using a ‘simple’

*localtransform.x += 1f;
(or any other equivalent pointer dereference token character, of course)

This way, I’d be able to manipulate “GameObject.Find(“target”).transform” without performing the Find operation again, and thus saving cpu cycles.

thanks

Place a local variable in your class, something like:

Transform someObject;

void Start()
{
   someObject = GameObject.Find("target").transform;
}

void Update()
{
  someObject.position = ...
}

someObject becomes effectively a pointer to the object you found using GameObject.Find

Also, if you make someObject public, you have the added bonus of being able to inspect it in the inspector and make sure it found the right object in your scene (for example), or you can drag the object in your scene onto the public variable reference, to populate the reference at design time.

All in all, a very flexible setup.