- Home /
Create a variable on a GameObject for access via the Object without GetComponent()
I've run into a bit of a problem with a game I'm working on. GameObjects tend to have a large number of variables that are easily accessed via the object itself, from TAG to TRANSFORM. I have a series of objects to which I need to attach a series of variables that constantly change during gameplay. More importantly, I need to access these objects at any time during said gameplay, and I have noticed to my frustration that constantly having to use GetComponent() slows down the entire game. Is there any way I can create a variable or use some sort of natural component so that I can simply grab what I need via the game object alone instead of having to deal with GetComponent() all the time?
To further clarify, constantly using GetComponent() is the equivalent of doing this: gameObject.GetComponent(Transform).position
What I want to do is this: gameObject.transform.position
The way I handle this is by using a base class. Derive it from $$anonymous$$onoBehavior, then derive every kind of would-be-monobehavior from the base class. I call $$anonymous$$e $$anonymous$$onoEntity for want of something better.
For each component you need to get frequently, code a property in your base class, like so:
private SomeCommonComponenet scc;
public SomeCommonComponent SCC {
get { if (scc==null) scc = GetComponent<SomeCommonComponent>() as SomeCommonComponent; return scc; }
private set { scc = value; }
}
Note that the expense of this convenience function is just as bad as (a little worse than, actually) "GetComponent" when the component is null, so be $$anonymous$$dful of that. When it's not null, though, this saves you the hassle of getting a reference each time it's necessary, and likewise saves performance by caching the result.
You say you want to get a variable but then you talk about GetComponent()
Couldn't you just cache the retrieved component in the Start() method? Then, using that cached reference, access whatever variable you need in the component without having to do GetComponent() all the time?
I would use either of the methods described by Sunny or Patrick, the answer below does not look at all right to me. You don't want to do what you said above :
"gameObject.transform.position"
This will be doing the same as gameObject.Find every time you access the transform component, it is slow.
Answer by FairGamesProductions · Nov 08, 2014 at 01:59 AM
The simplest way I found to transfer variables from any script to any other script, without depending on compile order or any of that mess, are empty GameObjects (for Boole variables), which I think you are also doing here. For example (JS):
private var MyBooleEmptyGameObject : GameObject;
private var BoolInThisScript = false;
function Start ()
{
MyBooleEmptyGameObject = GameObject.Find ("/Variables/MyBooleEmptyGameObject");
if (MyBooleEmptyGameObject.transform.localPosition.y > 0.5)
{
BoolInThisScript = true;
}
else
{
BoolInThisScript = false;
}
}
Of course, you don't have to use ANOTHER Boole in the script, but that is just for this examples sake. I usually keep all my Empty GameObject variables under another Empty GameObject called "Variables", to keep things neat. I also use the 0.5 Y position as the threshold between true and false, as sometimes in Unity GameObjects MAY change their position by 0.000000001 So using > 0.5 or < 0.5 is the safe bet.
The same process can be done with string, int or float.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171005im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Implicit downcast from 'Object' to 'String'. GetComponent from another object 1 Answer
Only change a variable on the instaniated object not the prefab. 0 Answers
Find + GetComponent not working as expected 0 Answers
updating a gameobject variable from another script attached to another object 3 Answers