Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ABNielsen · Nov 08, 2014 at 01:07 AM · gameobjectvariablegetcomponent

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlwaysSunny · Nov 08, 2014 at 12:51 AM 1
Share

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.

avatar image AlexanderPatrick · Nov 08, 2014 at 01:19 AM 1
Share

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?

avatar image MrSoad · Nov 08, 2014 at 02:13 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

get variable from a different game object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges