• 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 jusw85 · May 07, 2016 at 04:22 PM · script loading

Does order of scripts in inspector define execution order of Start()?

Unity 5.3.4f1, following the SpaceShooter tutorial:

https://www.assetstore.unity3d.com/en/#!/content/13866

https://youtu.be/n67w7aEZKhg?t=4285

Two scripts are attached to a ship:

  • Mover, which moves the ship downwards

  • Evader, which moves the ship from side to side

Mover code:

     void Start ()
     {
         GetComponent<Rigidbody>().velocity = transform.forward * speed;
     }

Evader code:

 void Start ()
 {
     currentSpeed = GetComponent<Rigidbody>().velocity.z;
 }
 
 void FixedUpdate ()
 {
     GetComponent<Rigidbody>().velocity = new Vector3 (newManeuver, 0.0f, currentSpeed);
 }

Key things to note:

  • Mover sets the velocity to (0, 0, -speed) in Start()

  • Evader grabs velocityZ in Start()

  • Evader changes velocityX, keeping velocityZ the same

In the video tutorial,

  • Ship objects are instantiated, and

  • Script order is Mover > Evader

and everything works. Ships move downwards, and from side to side.

However, if either:

  • Ship objects are placed in the scene, or

  • Script order is Evader > Mover

The ship only moves from side to side.

This suggests the following to me:

  • Start() order of scripts of objects placed in the scene is simultaneous; this matches the documentation at http://docs.unity3d.com/Manual/ExecutionOrder.html

    For objects added to the scene, the Start function will be called on all scripts before Update, etc are called for any of them. Naturally, this cannot be enforced when an object is instantiated during gameplay.

  • Start() order of scripts of instantiated objects is defined by the script order in inspector

I haven't seen anything in documentation to confirm the second point, though the video tutorial seems to rely on this. Does it work this way, can I rely on this behaviour? If not, how would I change up the above code to make it work reliably? http://docs.unity3d.com/Manual/class-ScriptExecution.html seems to only modify the script execution order of Awake, OnEnable and Update only.

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
0

Answer by Oribow · May 08, 2016 at 12:41 AM

You could move the GetComponent<Rigidbody>().velocity = transform.forward * speed; stuff into the Awake function. Then it would be guranteed to be excecuted before your other func. That would be the easiest solution. The general rule of thumb is, that any code accessing other scripts should be placed in Start and all other code, especialy setting up references to components.

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Loading a new level based on score 2 Answers

Would it be better to just combine these two scripts? 1 Answer

unity application load unity 0 Answers

Make a public slot to load the scene dragged in there? 0 Answers

Why does my footsteps in trigger script not work? 1 Answer


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