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 DevSkeletal · Sep 01, 2015 at 04:01 PM · c#findgameobjectswithtagcpucpu usage

using GameObject.Find and Cpu usage

So for the most part ive been using GameObject.Find for my audio masters, long story short im finding an object and referencing a specific script attached to that object, however ive heard that GameObject.Find consumes a lot of cpu and its much better to find with tags which i have been doing for the most part

Anyways, for this problem i need to use find for reasons that would take to long to explain but ive found a workaround and i think it might be less consuming on the cps, let me know

 private GameObject spikesound;
 
 void Start () {
 
 GameObject AudioObj = GameObject.FindWithTag("AudioMaster");
 spikesound = AudioObj.transform.Find ("TimedSpikeAudio").gameObject;


will this be less consuming of my cpu? thought id querry it with you before i delved into changing everything

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
0

Answer by Baste · Sep 01, 2015 at 04:14 PM

The reason for why it's expensive to use Find is that Unity has to look through all of the objects and check their name. The looking part is the bad one.

Tags are a bit better - there are only 32 possible tags, so (I assume) there are some dictionary-like structures that maps from tags to objects, which would make it a lot faster to search.

So, since you're only doing this in Start, the cost of the FindWithTag and Find operations only happens once, so this should not be a problem. Unless that Start method is called a lot - like if this is on a prefab that you create many copies of.

Still, directly referencing the object is faster. Why not just have the spikesound object as a public variable, and assign it in the inspector? If you're creating these objects at runtime, that's not possible, but otherwise it's often the easiest to work with, and the fastest.

What script is this code on? Do you have any script on the spikesound object? Both of those will help me give you better answers.

Comment
Add comment · Show 2 · 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 Eric5h5 · Sep 01, 2015 at 05:07 PM 0
Share

There's no limit on the number tags that I know of. You're thinking of layers, but those are just 32-bit numbers.

avatar image DevSkeletal · Sep 05, 2015 at 12:31 AM 0
Share

Thought i replied a dew days back but guess it didnt send :p

this script in particular is referenced in most of my game mechanics (such as spikes moving up and down in this case) which play sound through an audio master to stop them overlapping, the main reason im querying about this is because i may have hundereds of these spike mechanics in my level, and each one of them has to find the audio master for it, now they all do this in start but someone mentioned i might get a few secconds of stuttering if i just use gameobject.find, i thought the method i posted, which finds with tag first might help aleviate that

hope to hear back soon and thanks for your help

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Detect clone using FindGameObjectsWithTag method. 1 Answer

Does using Mesh Collider use up more CPU verses just a box Collider? 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