• 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
2
Question by gregzo · Jun 26, 2012 at 09:28 AM · instancesoop

Many instances of a small script, or reference to objects in one master script?

Hi to all!

Here's my use case, but the question is more general:

In my scene, I have 100 instances of a sprite. All sprites already hold an instance of SpriteScript (detecting touch). Now, which is better, both performance wise and coding wise (app is for iPad):

1) To expand SpriteScript with other sprite related functions (i.e. move there, scale, calculate position, etc..) Consequence : many instances of a larger script

2) To send all needed info from sprite to a SpriteController script, which references all sprites? Consequence: keeps the sprite script small, but more coding and more difficult to maintain.

Many thanks!

Comment
Add comment · Show 2
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 AlucardJay · Jun 26, 2012 at 09:59 AM 0
Share

I likie, thumbUp and bump =]

personally, I have been using option 1, did funny things when separate instances called $$anonymous$$anager script when using common or cache vars in the $$anonymous$$anager functions (like both calling function "move there" at the same time).

i also think not using Update in the instances makes it ok, just a scattering of 'helper' scripts, with references to them from instantiation by $$anonymous$$anager to fire off their functions when needed.

avatar image gregzo · Jun 26, 2012 at 10:45 AM 0
Share

So far, i've been doing a bit of both. If there are many instances of a script, I tend to assume keeping the script shorter could help performance... Hoping one of the higher gods steps in!

1 Reply

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

Answer by Bunny83 · Jun 26, 2012 at 10:43 AM

If a script or class contains a lot of code, it doesn't matter how many instances you have of that script. The code for a class only exists once. Member variables are eating up memory since they exists on every instance. It might be a little bit faster to handle everything in one script and loop through your instances, but in most cases that's neglectable.

It's by far more important to optimise the rendering. Hopefully you already use some kind of sprite-toolkit that batch them into one drawcall.

Unless you have real problems with the performance of code execution, just code logical. In OOP a class is written to serve one specific task.

If you have Unity pro, you can check the cpu usage for all modules in the profiler. Unless you have big hiccups in the code section keep the classes seperated ;)

Comment
Add comment · Show 4 · 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 gregzo · Jun 26, 2012 at 10:48 AM 0
Share

There! Thanks for the detailed reply. Using NGUI, can't believe I did without for so long... 1 draw call in my scene, yippee! Plus all the goodies that come with it.

Cheers, Gregzo

avatar image AlucardJay · Jun 26, 2012 at 11:03 AM 0
Share

$$anonymous$$any Thanks (making my own meshes and swapping uvs on the verts).

So to confirm, a class on each instance is only lagging everything by the amount of vars (public and private) times number of instances. And so including an Update function and/or with detecting touch inputs is also negligable?

avatar image Bunny83 · Jun 26, 2012 at 11:24 AM 0
Share

Well, of course every function call cost a little bit performance, but it's really not that much. $$anonymous$$ember variables doesn't really influence the performance in the sense of speed, they only eat up memory ;)

avatar image AlucardJay · Jun 26, 2012 at 11:38 AM 0
Share

Thanks again. Self-taught so sometimes ter$$anonymous$$ology is lost on me. I also now see the difference and considerations for performance (FPS) and memory (memory usage, number of apps running etc)

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

6 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Update Function Work Around Question 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