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 Sahnvour · Mar 06, 2013 at 12:22 PM · c#gameobjectcomponentaccess

Is it OK to use public accessers for unique gameobjects (C#) ?

Let's say I have several game objects that are unique in my game, and that will always be present. For example the main character.

Its behaviour is implemented in a C# script, deriving from MonoBehaviour. Imagine now that it has some components like particle systems, line renderers or whatever attached to it as gameobject childs.

One way of accessing the main character's particle systems from another script is to do something like

 GameObject.Find("My character").GetComponentsInChildren<ParticleSystem>()

It works, but I would like to know if it is a better solution to have (static if needed in some cases) public accessers or properties to do this kind of access. For example, storing the particle systems in an array in the Start() method of the main character and provide access to it by a property.

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

3 Replies

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

Answer by GuyTidhar · Mar 06, 2013 at 12:31 PM

Without a doubt you should do one of two things:

  1. Look once ('Find' & 'GetComponent') and store for the rest of the objects life in private variable.

  2. Drag the references by hand before run time to public variables.

Calling 'Find' and 'GetComponent' every time you need it from scratch can hinder you performance. Not to mention calling these every frame which can kill your performance like you wouldn't believe it.

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
avatar image
0

Answer by Landern · Mar 06, 2013 at 01:48 PM

GuyTidhar is completely right, if you can capture the reference of an object and hold that reference, you win. The big thing to remember here and you totally control this, if you destroy anything, the reference(s) are going to be toast and require you to update any references, otherwise you may end up with null reference exceptions all over the place.

 private GameObject playerChar;
 private List<ParticleSystem> playerPartSys;
 private LineRenderer playerLineRenderer;
 
 void Start()
 {
   playerChar = GameObject.Find("My Character");
 
   if(playerChar)
   {
     playerPartSys = new List<ParticleSystem>(playerChar.GetComponentsInChildren<ParticleSystem>());
 
     playerLineRenderer = playerChar.GetComponent<LineRenderer>();
   }
 }
 
 void Update()
 {
   // Do stuff with vars
 }
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
avatar image
0

Answer by Owen-Reynolds · Mar 06, 2013 at 04:39 PM

How often do you use them?

If it's for something that happens at most every few frames (explosion, scoring, spawn enemy...) it can be easier to just do a 1-line gameObject-Find. Much easier to track down errors. I have some non-trivial stuff that runs just fine on an iPad which is full of GameObject.Finds (but not in Update.)

If you feel weird about being sloppy, look up "premature optimization." Common rules are not to speed up code if it runs fast enough already and focus on speeding up the parts that run the most (ex: once per enemy per frame, code in loops.)

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

13 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

Related Questions

Find a Component/GameObject Using an Interface Reference 2 Answers

How to replace a gameobject referenced in JS using C# 2 Answers

Access water4 scripts via my script? 1 Answer

Cannot destroy Component while GameObject is being activated or deactivated 2 Answers

How to activate/deactivate a component(Animator) when a specific game object was activated 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