- Home /
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
Answer by runonthespot · Jul 14, 2011 at 10:19 AM
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.
thanks for the suggestion, but I realize that it only works for gameobjects, since they're pointers by themselves. Since I'm trying to maximize performance by reducing the number of "Find" and "FindChild" calls I need in my subroutines, I need to be able to bypass the very GameObject, and access its properties directly after I've "pointed" at them. Is there maybe an advanced pro trick that'd allow 'unsafe' access to variables?
Surely accessing 100 pointers in a scene would be incredibly faster than making 100 "FindChild" calls, wouldn't it?
I'm pretty sure that's wrong- it should work for transforms and in fact any component type in Unity (anyone more knowledgeable feel free to step in and agree/disagree). In many places you'll find code that caches references to transforms, rigidbodies, colliders - pretty much anything that derives from $$anonymous$$onoBehaviours.
you are right. there was a case that led me to believe otherwise: i had a non-static function in a static class, and it would not receive a parameter by ref. converting it to static resolved the issue
However, as a matter of fact, in that case i needed to use the 'ref' keyword, both in the call and in the declaration.
Yes- the ref in the call is a C# oddity I'm only just used to doing.
Your answer
Follow this Question
Related Questions
Having script accessing issues 2 Answers
Scriptable object saves but what about objects it references? 0 Answers
Do you have to set a object reference for every script? 0 Answers
Is a unity object really destroyed if its destructor was never called? 1 Answer
The infamous: Object Reference not set to an instance of an object 1 Answer