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 frankyboy450 · Jan 28, 2014 at 10:45 PM · c#loadingstartcacheoptimize

"Caching" code in the Start() to run it "later"

I have a lot of code in the start function of my script and all of it is essential for the game to work properly, and when I run it on my phone, it lags for a second or 2 until it's done. Isn't it better to cache all that code in a list and run each line every 10th frame or something and have a small loading screen instead?

Comment
Add comment · Show 6
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 RyanZimmerman87 · Jan 28, 2014 at 11:43 PM 1
Share

I think you may be able to fix this by just having a loading screen if you aren't using one. I believe the loading screen will display until the Start() functions are complete?

Not sure if it's worth getting too fancy with Start() but then again I don't know anything about your project.

avatar image KellyThomas · Jan 29, 2014 at 01:08 AM 1
Share

Two questions that come to $$anonymous$$d are:

Is the same work being done by several instances of the same script? Could they share the results?

Can some of this work be performed with Lazy Initialization?

avatar image frankyboy450 · Jan 29, 2014 at 01:49 PM 0
Share

A loading screen could probably work, so it waits like 2 seconds before removing it and starting the game...

It's all being done by the same script, it adds all the ground objects in the game(2D game) to a list, and changes some color on objects.

What's lazy initialization?

avatar image KellyThomas · Jan 29, 2014 at 02:11 PM 1
Share

Lazy initialization is technique to put off work/research that may not be needed until you know that it is, here is one example:

 private GameObject[] _players[];
 public GameObject[] Players {
     get {
         if (_players == null) {
             _players = FindGameObjectsWithTag("Players");
         }
         return _players ;
     }
 }

 // Then your code can access Players knowing that it will always return a valid array.

When used appropriately putting of this work until it is actually needed will spread it over several frames so the player will not experience any upfront delay.

It doesn't look like it would be a good fit in your case. A loading screen sounds easiest, and/or some optimisation might be possible?

avatar image KellyThomas · Jan 30, 2014 at 09:13 AM 1
Share

I wouldn't have a time based loading screen, dismiss it when your work is done and the level is ready to play.

Also be aware that coroutines are effected by the timescale.

Show more comments

3 Replies

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

Answer by komodor · Jan 30, 2014 at 10:38 AM

maybe you need to put some code to Awake, because Start is the first frame of GameObject (the enable exactly), while Awake is something like onLoad

here is the list of all methods http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html

if you need to distribute some code for later, you can use coroutines http://docs.unity3d.com/Documentation/Manual/Coroutines.html or you can just use Update function to call it later

Comment
Add comment · Show 1 · 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 frankyboy450 · Jan 30, 2014 at 01:02 PM 0
Share

Thanks, do I can pause the game in awake and run all my code there, and then un-pause it in start.

avatar image
2

Answer by Nanocentury · Jan 28, 2014 at 11:02 PM

This can be achieved with Co-Routines: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines

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
avatar image
1

Answer by DynamicPrgm · Jan 28, 2014 at 11:56 PM

You could use another function?

 void DoThis {
      //code
 }
 
 void Start() {
      DoThis();
 }

 bool youneedthisnow;
 void Update() {
     youneedthisnow = true;
     if(youneedthisnow){
          DoThis();
     }
 }
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 Bunny83 · Jan 30, 2014 at 10:23 AM 0
Share

How does this change / optimise anything? O.o

Also what's that for an if statement outside of a function? That wouldn't compile.

avatar image DynamicPrgm · Feb 01, 2014 at 02:39 PM 0
Share

I don't know, I was just suggesting a possible solution. I know this won't compile, I'll fix it now. It was just an example :P

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

23 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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Save and Load Prefab and Mesh 1 Answer

OnTriggerEnter is called too late (after Start) 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