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 Ranger-Ori · Sep 10, 2012 at 09:50 AM · gameprogrammingefficiencydevelop

How to be efficient?

This troubles me, when I see other people projects, everything looks like cogwheels connected one to each other in such a perfect way. While I work on my project, I always find myself changing and reinventing my work in a different way so everything will fit.

I'll give you an example:

  • If I for instance want to create a player, who can shoot. I can perform shooting in two ways, wether to instantiate a prefab, or using raycasting. Each of these options will resolve as a chain reaction in future work. and if I suddenly decide I need a different approach in my game, I need to rewrite everything.

  • In case I want to create a health manager, should I add it for AI's? or just the players, and then recreate a similiar script with health to my AI.

By the end of the day, I'll have 20 scripts, and I haven't finished my work yet. I feel I'm doing something wrong, and I don't know what to do in order to fix it, maybe it because my lack of knowledge in programming. I know how to write, I'm using UnityScript by the way, it's just that I feel it is too messy.

How can I solve this problem? It's not just a specific problem I need to think of, it's my whole attitude for game developing.

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
5
Best Answer

Answer by Kryptos · Sep 10, 2012 at 11:36 AM

To be efficient, consider this statement: divide and conquer. You need to divide your object into smaller part that are self sufficient. You can then reuse these parts more easily.

Unity enforce this rule because objects are component-based. Each component should be responsible for a particular task and nothing more. It is better to start with too much small parts than a big fat script with 1000s of lines.

Consider an enemy bot. This bot can move. It can fire bullets. And it is also a kind of AI. You could use a unique script to do that. But I recommend to make three scripts:

  1. The Locomotion script: its task is to move the enemy bot.

  2. The Gun script: it controls the gun and the bullet that are fired.

  3. The Brain: it contains the AI algorithm. This is the main script that will use the Locomotion and the Gun scripts when needed.

Advantages of this approach: you can reuse the scripts on other objects. You can even switch one script for a more specific one without recoding everything. For example, the big boss enemy bot with use the same Locomotion script, the same Gun script but a different Brain.

Of course, if you have too much small parts, it can become a mess to maintain. And it is not always good for performance. But keep in mind than performance is an issue only when you ship your game. It is a waste of time to try to make the most efficient scripts at the beginning because most of the scripts will be modified or thrown away. So at the end of the development, you can consider merging very small scripts into bigger scripts. And this operation is very simple: just copy paste everything into a new script. On the other hand, splitting a big script is very difficult and error prone.


Back to your examples.

I can perform shooting in two ways, wether to instantiate a prefab, or using raycasting. Each of these options will resolve as a chain reaction in future work. and if I suddenly decide I need a different approach in my game, I need to rewrite everything.

Write a script for each way. Write a base script that contains all common code shared by these two ways. Make each script inherits the base script. In the rest of your project, only use base script reference. Whenever you decide to use a way or the other, you just change the script used without modifying the rest of the code.


In general, try to learn the basic object-oriented design patterns. The bible in this domain is the book from The Gang of Four which title is... (I will let you guess...) Design Patterns.

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

10 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

Related Questions

Multiple Cars not working 1 Answer

need help with a script 1 Answer

How do i make an animation play on key press? 3 Answers

Bullet Fire script not working 1 Answer

Getting Error "No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(UnityEngine.GameObject)' was found." 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