• 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
Question by megabrobro · May 02, 2018 at 02:13 PM · unity 5referencing

What Alternatives are there for GameObject.Find()

Hi all

I have just jumped back into using Unity after several months away. I'm looking for ways to store a reference to the UI panels I have made.

So far, i remember two ways, but I would like to know of other ways to store the reference so that I can use it in whichever class I feel like.

Option 1: GameObject.FindObjectWithTag("xyzScreen") - this of course can be used and I can create a local variable to hold the object that is returned. This works fine as long as it has a unique tag (I remmeber running into problems using this once I started making multiplayer games in past.)

Option 2: Drag n drop (inside Unity Inspector) a reference to each object into Public variable fields I made in the classes I want to use the screens with. nb. I hate doing this, I'd rather work 99.99999% in code if at all possible. And not to mention quite often the Inspector fields just become empty again and I have to drag references again. This to me feels like a workaround for not knowing the correct way to do this.

Option 3 (I know I said 2!) : Create the screens completely in code, and therefore instantiate and make active/inactive using variables I made whilst creating them.

So for my project today, which has led me to ask this question is: I have a game with 4 possible 'areas' (all that means is the main viewport has 4 panels, Gym, Office, Home, Fightnight). I have a GameController.cs class which will be responsible for opening and closing each panel upon Button presses.

so my GameController.cs needs references to each of the UI panel objects.

I hope that makes sense and somebody can tell me the correct way for storing such references

THANKS

Comment

People who like this

0 Show 0
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
Best Answer

Answer by RadonRaph · May 02, 2018 at 02:44 PM

You can use simply GameObject.Find("ObjectName"); if all of your 4 panel have differents name this will work. Also if you script is in parent gameobject of your panels you can use Transform.Find("ObjectName").gameobject.

Comment
megabrobro
ZuhairGhias

People who like this

2 Show 3 · 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 tormentoarmagedoom · May 02, 2018 at 03:05 PM 2
Share

Good day WhiteCry & megabrobro.

Just as a tip, GameObject.Find, what does i go through ALL GameObjects in the scene, all of them from the bigest aprent to the smallest child, to check if the name is equal to the one given by you. As you can imagine, this is serveral times more work for the cpu, than all others options. So, i strongly recommend to not use GameObject.Find in your scripts, (maybe you have no ther option), but as a good practice, is better to create a tag and look it by the tab, or assign it in the inspector.

Bye! :D

avatar image BastianUrbach tormentoarmagedoom · May 02, 2018 at 08:36 PM 0
Share

Agreed, GameObject.Find is not a good option for something that happens every frame. What would usually work fine however is using it just once in Start and then keeping the result in some variable for the rest of the game.

avatar image TreyH · May 02, 2018 at 08:42 PM 0
Share

I don't see how the solution to a question asking for alternatives to GameObject.Find is a recommendation to use GameObject.Find...

avatar image

Answer by megabrobro · May 02, 2018 at 02:55 PM

ok thanks mate. I was kinda aware of those, but for some unexplainable reason I thought that this was sort of the wrong way to do it. Glad to know that its probably just me being silly. I can of course easily name or tag each screen with a unique string.

I will do that for now, and in future when I doing multiplayer again I can use a different technique for 'tagging' other game objects. (I remember I used the Find("") methods etc and once I networked my game the screens would popup on everybodies screen because it was just finding all of them. The same was said for animations and other objects (my memories a bit hazey to say the least :P)

Thanks for your help though I think I can get on with it for now :D

Comment

People who like this

0 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 RadonRaph · May 02, 2018 at 03:01 PM 0
Share

Ive never done multiplayer, but i think i will put a function in every player that rename the player "Player + unique id" and then hook with the rest(gamecontroller etc). and so in my gameController there will be Players[Player351, Player425 etc]. That only my thought x) But i'm like you i use 99% code, so i use GameObject.Find().

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

169 People are following this question.

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

Related Questions

Is it possible to give a color to int variables? 1 Answer

How to create a turn based battle system? 3 Answers

How to use #extension with GLSL shader 0 Answers

Is there a Wild Card Variable in Unity c#? 5 Answers

quit game if no internet access on android 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