• 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
-1
Question by adit1 · Apr 03, 2013 at 05:26 PM · 2doptimizationplatform

Anyway to get the scripts associated with particular game object?

Hi, Is there anyway I could get every script associated with a game object? I want to access it from script of another object. The idea is to optimize the performance by deactivating scripts. Or is there a better solution for optimizing the objects that are in distance from player? It's a 2d platformer. Thanks!

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
1

Answer by Jessy · Apr 03, 2013 at 06:18 PM

The best solution is to have each script disable itself as appropriate. Managing a bunch of scripts like this is not good, maintainable coding practice. That said, here:

 [SerializeField] new GameObject gameObject;
 
 foreach ( var script in gameObject.GetComponents<MonoBehaviour>() )
    script.enabled = false;

Every script that you can attach to a Game Object is a MonoBehaviour.

What you should do, is, track the circumstance that will lead to the mass disabling in one script. Fire an event upon that circumstance. Register for the event in all of the scripts that you care to disable.

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 adit1 · Apr 03, 2013 at 07:06 PM 0
Share

What about the terrains in 2d platformers? Like they effect the performance? If I deactivate the entire object, the objects on the terrain would fall off. How do they do in traditional 2d platformers?

avatar image Jessy · Apr 04, 2013 at 05:45 PM 0
Share

I don't know what you're asking, but it sounds like a different question, suitable for the forum.

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

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

HUD with a lot of elements, efficient way? 3 Answers

How can i create a 2D polygon map for my infinte runner game 0 Answers

2D Sidescroller enemy AI jump help! Picture Included! 3 Answers

gfx.waitforpresent took up to 40ms even without any gameobject running 0 Answers

What's more optimized? Diseable an sprite renderer or diseable the GameObject that contains the SpriteRenderer? 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