Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 roamcel · Jul 14, 2011 at 07:10 AM · objectreferencepointeraccessing

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

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · 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
avatar image roamcel · Jul 14, 2011 at 11:37 AM 0
Share

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?

avatar image runonthespot · Jul 14, 2011 at 11:47 AM 0
Share

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.

avatar image roamcel · Jul 15, 2011 at 05:19 AM 0
Share

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.

avatar image runonthespot · Jul 15, 2011 at 08:12 AM 0
Share

Yes- the ref in the call is a C# oddity I'm only just used to doing.

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

3 People are following this question.

avatar image avatar image avatar image

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


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