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 /
  • Help Room /
avatar image
0
Question by mike1233 · Feb 05, 2017 at 12:20 AM · scripting problem2d gameoptimization

is it more optimal to use 2 short scripts or 1 long script?

is it more optimal to use 2 short scripts or 1 long script?

does it make any difference and if so how can you prove this?

please only respond if your sure

so for the player is it more optimal to have 2 scripts short or a longer one or does it make no difference?

2d game

Comment
Add comment · Show 3
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 Pengocat · Feb 05, 2017 at 03:12 AM 1
Share

Depends heavily on how you communicate between the two scripts. If you do it efficiently by reusing an existing reference the difference would be negligible. Every time you use Vector3.zero is equivalent to calling a method in another script. Having one long script makes it less reusable though.

avatar image mike1233 Pengocat · Feb 05, 2017 at 09:39 AM 0
Share

thanks but im a little baffled by your wording less reusable?? is that good or bad

also what do you mean communicate between the 2 scipts? they don't communicate with eachother they are separate fucntions for this example each are a oncollision functions with different collision gameobjects

so is it more optimal overall to use along script or short ones

its a 2d game would I ever use vector 3.zero?

and rite now the short scripts include oncollision function for enemy and player etc so for example player dies when he hits certain things and or walls so I made 3 separate short scripts for each on collision s since each collider has a different tag

and please tell me from your experience --- overall is it more optimal to use many short scripts or a long one?

avatar image Pengocat mike1233 · Feb 05, 2017 at 01:41 PM 0
Share

It is usually bad if you can't reuse a script since if you need a variation of your script you need to copy paste some parts, which introduce more maintenance. It is however also bad if you over complicate things only to future proof it especially in games since you normally have a very short time to complete things. Refactoring the code when needed is usually what I do.


The Vector3.zero example was just an example. The same is true for Vector2.zero and other static variables and methods.


Since Unity is mostly component based it makes sense to continue that ins$$anonymous$$d of setting up a lot of dependencies. This often means that you get smaller scripts, however they should not be so tiny that they could be anything. The performance would perhaps suffer a little bit but it is often outweighed by the productivity time. Like most game developers experience it the majority of the code for a game is not performance critical. Perhaps 2% to 10% of the game should be heavily optimised while the other 90%+ could perhaps yield a few percentage increase in performance if optimized.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Chikari · Feb 05, 2017 at 10:55 AM

This depends on what your definition of "optimal" is.

As @Pengocat mentioned, if you put more functionality into one script, it becomes less useable. Say you have an object A_1 that checks for collision with B and C and an object A_2 which should only check for collision with object B. You could drag both scripts CollisionB and CollisionC onto A_1, and only CollisionB onto A_2. This would not not be possible if both collision checks were in one script (..without modifications..).

If you are referring to performance: If you split functionality into two scripts, you're going to have two function calls. Script1.doSomething() and Script2.doSomething(). A function call does require "some time", so having two is slower. But that overhead is extremely small. Also, the compiler might "inline" both functions. In this case, the performance difference is basically zero.

However, if you split up functionality into too many files, maintenance might get messy. Say you want to change the way doSomething() does it's thing: If it's in one file, you can change it alltogether. If it's in multiple files, you might miss something out.

So concluding... it really depends on your script's functionalty, how often it's going to be used and how many scripts you have. Performance can be neglected imo.

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

120 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

Related Questions

Error: An object reference is required to access non-static member, help? 2 Answers

my game wont end when no enemy is killed 0 Answers

Dynamic Batching for SpritesRender 2 Answers

How do you manage the amount of time Ads appear in your app? 1 Answer

Handle order in layer in 2D game ? 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