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 naqvir · Mar 28, 2012 at 09:25 PM · javascriptgameobjectbeginnergetcomponent

Best Unity Practices

Hi everyone,

This is a very general question that i am going to pose to everyone in unity answers.

What are the best practices in getting the most efficient code output for the cheapest amount of processing, when writing javascript code in unity?

I ask this question for many reasons, but mainly for beginners like myself. I want to learn unity the best way with a good cognitive logical structure. Personally i do not like trial and error learning because you can pick up bad habits that can create code inefficiencies. I am a university student, and i have learnt java and javascript before, but the way unity works especially with the different update methods confuses, because i am not dealing with static objects mostly as the page updates every x amount of frames per second.

I recently learnt that you should never write a "GameObject.Find("String")" in the update, it is better to do GameObject.Find(String) (no quotes), and that when retrieveing specific componants from an object do this:

 var controller:GameObject;
 **private var getEssenceHandlerComponant:GetEssenceHandler;**
 private var valueGuiText:GUIText;
 
 function Start() {
 **getEssenceHandlerComponant = controller.GetComponent(GetEssenceHandler);**
 valueGuiText = GameObject.Find("Value").guiText;
 }

Instead of this:

     public var controller:GameObject;
     **var getEssenceHandlerComponant:Component = controller.GetComponent("GetEssenceHandler");**

     function LateUpdate () {
     var number:float = getEssenceHandlerComponant.lifeEssenceCarrier;
     number = Mathf.Floor(number);
     gameObject.Find("Value").guiText.text = number.ToString() + "%";
     }

But anyways those are just examples of better ways of doing the same thing for the cheaper price. Please do not hesitate to give some advice guys. We are all here to help each other in the end. :)

Thanks in advance

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
2

Answer by hijinxbassist · Mar 29, 2012 at 09:23 AM

Here is a good place to start http://unity3d.com/support/documentation/ScriptReference/index.Performance_Optimization.html>Performance Optimization. There is a full discussion with examples http://unity3d.com/support/resources/unite-presentations/performance-optimization.html> here.

Type all vars to avoid any dynamic typing. Using strict typing is a good way to slim down. #pragma strict.

 var color=Color.green;
 var color:Color=Color.green;

Avoid calling things from Strings if at all possible. Avoid Array(), and use []

 var array:Array=new Array();//creates empty array
 var colorArray:Color[]=new Color[5];//creates a color array with 5 empty slots

Avoid Update calls wherever you can, including similar constantly updating functions...OnGUI(),FixedUpdate(),LateUpdate()

Best thing is structure, avoid redundancy and unnecessary calls.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

press e to speek -1 Answers

Add force to GameObject 2 Answers

enable and disable boxcollider (whats wrong with my script?) 2 Answers

Assets/Scripts/Cubemove.js(44,167): BCE0017: The best overload for the method 'UnityEngine.Mathf.Round(float)' is not compatible with the argument list '(boolean)'. 1 Answer

How do I reset a variable? 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