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 daviddickball · Jun 17, 2014 at 08:35 PM · javascriptfind

GameObject.Find() sometimes fails, is that okay?

I have several invisible locations that my player has to walk over to complete the game. When she touches one, the next will highlight. I do this by using Find() to find the current helper, ie:

 var currentGoalHelper = GameObject.Find("Goal"+currentMissionNum+"Helper");

This works great until the final goal is reached because obviously there are no more to find, so I get a Unity error in the console. Is this okay? Or should I be doing some sort of check first? Or is it okay if a Find() fails? I assume sometimes it just won't find anything right?

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 Owen-Reynolds · Jun 18, 2014 at 12:31 AM 0
Share

The trick is, the Find isn't causing your error. Using the bad Find is causing it.

If you reread the two responses below, you'll see the sequence is: do a Find, check if it worked, use it if it did.

avatar image Kiwasi · Jun 18, 2014 at 12:36 AM 0
Share

$$anonymous$$eeping error messages out of the console is important for framerate, at least in the editor. Unity is probably smart enough to ignore errors on a build, but I wouldn't count on it.

avatar image Owen-Reynolds · Jun 18, 2014 at 12:59 PM 0
Share

??? Run-time errors in a build cause a crash or freeze, at least in iOS. The final version of the game must never cause a single red error.

But, as the OP has experienced, an error or two while running in the editor isn't that big a deal. You have to fix them sometime, but not right away. Not unless something is spraying errors. If you aren't sure what to do when they finish the race, an error is as good as anything else, for now.

2 Replies

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

Answer by Kame Sama · Jun 17, 2014 at 09:12 PM

Yes it's okay if the find fails. To keep your console clean, you can wrap the var currentGoalHelper with a check before trying to access it like :

 var currentGoalHelper = GameObject.Find("Goal"+currentMissionNum+"Helper");
 
 if (currentGoalHelper){
     //Execute code
 }else{
     //Don't access currentGoalHelper as it is null
 }

Keep in mind that the GameObject.Find function will loop across all your gameObjects in the scene. I would personally keep a reference to your currentMissionNum in a static class to avoid running the GameObject.Find every time you need to access the currentMissionNum.

hope this helps!

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 daviddickball · Jun 17, 2014 at 11:09 PM 0
Share

Thanks but how will I know the Find() will return false unless I check it?

avatar image
0

Answer by Landern · Jun 17, 2014 at 08:38 PM

use an if statement to constrain the code, if currentGoalHelper is null skip the code that is associated withe currentGoalHelper, if it is not null, do what you normally do.

 // .. code
 if (currentGoalHelper) {
 ... code that uses currentGoalHelper
 }
 
 // .. code
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

Get all Game Objects near point 2 Answers

Separate objects from GameObject[] group 0 Answers

How to access audio of FindChild Object & play it? Error 0 Answers

Problem with finding animator. 1 Answer

Finding A Rigidbody's Rotation Speed And Direction 2 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