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 DFiable · Jan 09, 2012 at 11:39 AM · androidperformanceoptimizationfind

Is there a more efficient way to write a "Find" script?

I'm working on Android Unity Pro. My game has hundreds of "Find" scripts and I trying to optimize performance. Is there a better way to avoid writing a "Find" script? Below is a typical "Find' script in my game. If someone could teach me how to write it more efficiently I'd appreciate it. thanx

 function OnTriggerEnter( myTrigger : Collider){
       if(myTrigger.gameObject.name == "tum1-2"){
       var myTryagain : CameraControls = gameObject.Find("Main Camera").GetComponent(CameraControls);            
             myTryagain.enabled = false;            
         }
 }     
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by syclamoth · Jan 09, 2012 at 11:42 AM

You really shouldn't have to use GameObject.Find ever, under any circumstances. If you ever find yourself using it, have a good hard look at what you're doing and make sure there's not a better way.

For this specific example, is there any reason why you can't just keep a reference to that 'CameraControls' object in your component, instead of finding it every time you enter a trigger?

Comment
Add comment · Show 10 · 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 DFiable · Jan 09, 2012 at 11:49 AM 0
Share

I don't understand. How do I avoid it?

avatar image DFiable · Jan 09, 2012 at 11:50 AM 0
Share

This script is on a different game object.

avatar image DFiable · Jan 09, 2012 at 12:03 PM 0
Share

What do you mean by a "reference"?

avatar image syclamoth · Jan 09, 2012 at 12:19 PM 0
Share

A 'reference' is a variable through which another object can be accessed. It's a pretty common program$$anonymous$$g term.

If the script is on a different game object, just have a variable

 var myTryagain : CameraControls;

and set it up in the inspector, at the very least. Then you don't have to use gameObject.Find.

Or, you can keep a reference to the 'main camera' object, and use that.

Either way, you never have to use GameObject.Find. I cannot thing of a single situation in which it is the correct thing to do.

avatar image CHPedersen · Jan 09, 2012 at 02:03 PM 3
Share

I upvoted this reply out of pure spite - The ratio of experts to newbies at Unity Answers is about as ridiculous as the US budget deficit, so downvoting one and then creating a new question with one of the experts' name in it just to attract attention is not the way to go. :-(

On a technical note, I don't agree entirely, though. ;-) I do feel there are situations where GameObject.Find is acceptable, such as caching references to gameobjects on startup. I realize you can use the inspector to set up variables, but I think this approach obfuscates the assignments that take place at runtime. Code should be self-explanatory on its own - you should be able to glean from the source itself where the value of each variable comes from. Declaring a variable like so: "var something : float", and then setting its value in the inspector makes the variable appear uninitialized in the source file.

In addition to this, don't variables have to be public before you can edit them in the inspector? From an OO point of view, forcing public access is an antipattern.

Show more comments
avatar image
3

Answer by Jodon · Jan 09, 2012 at 02:45 PM

Having a public reference to your Camera certainly solves this issue as syclamoth has stated. However, you're actually looking for the CameraControls component, so having a public variable in your class that points to the component is more efficient in this case.

Another approach would be to write an Awake() or Start() function and use gameObject.Find in there, save the results to a class variable (preferably a private variable), and reuse that variable in the OnTrigger. This will ensure your Find() performance hit occurs only once, not every frame you're in the trigger.

A third way would be to use Camera.main which is a static variable that always points to the "main" camera. http://unity3d.com/support/documentation/ScriptReference/Camera-main.html. Depending on Unity's implementation, this could also be a bit slower than the techniques mentioned above. You could however combine it with suggestion #2 (the Awake()/Start() caching).

Finally, you should probably avoid the .name == "tum1-2" string comparison as string compares can be slow. You could use the same suggestion of using a public variable to store the object "tum1-2" that you're looking for. I would think this particular code actually belongs in another class though, one where you know you're dealing with tum1-2 directly (e.g. should be in a behaviour that attaches to tum1-2) then you can avoid the whole name comparison/object comparison issue all together.

Sorry I didn't write any code for you but I don't write in JavaScript and would not want to provide you with non-working code.

Comment
Add comment · Show 1 · 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 DFiable · Jan 10, 2012 at 04:30 AM 0
Share

Thank you!

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

8 People are following this question.

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

Related Questions

Is there a more efficient way to writ a "Find" script? 0 Answers

Finding inactive game objects in a pool 1 Answer

How does gameobject.find() loop through the hierarchy? 3 Answers

On Android. How many lights can you have in a Scene or the total game? 1 Answer

[Android] Significantly Worse performance on better device - 64bit CPU problematic? 0 Answers


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