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 1337GameDev · Jul 07, 2012 at 12:05 AM · c#nullstartobject referenceexecution order

Order scripts execute "start" or order of "update" every frame, c# mainly

I have 2 scripts,a utility script that is meant to list all gameobjects that might be used by others (to avoid lots of searches) and a script that wants to use this script and use a gameObject's script component reference.

I have both of my code in start, but when i check the inspector the utility script finds the gameobjects fine, but my 2nd script cannot access them. I try to set a variable to the object reference from my utility script and it shows up as null in inspector. The utility script has this reference fine. When i move the assignment statement to "update" it successfully finds the reference and assigns it from the utility script.

This makes me believe that my start method for my 2nd script is running before my utility script gets a reference. How do i change in what order these two scripts execute so that all my scripts can fetch object references from the utility script AFTER utility script is done executing, and hence avoid null references?

If you need more info just ask. I hope i gave enough. PS: My script code to locate the utility script works fine, just returns null when it is in "start" method.

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

2 Replies

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

Answer by Llama Fragments · Jul 07, 2012 at 12:56 AM

Try changing the execution order of your scripts so that the utility script runs first, pretty sure that's under Edit/Project Settings/Script Execution Order

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 Moohasha · Jul 07, 2012 at 12:27 AM

I don't think you can control the order in which scripts start or are updated, though you can to some degree by using Update(), FixedUpdate(), and LateUpdate() appropriately. In your case, I would have a method in the utility script to return the list of GameObjects rather than just making the list public, and the first time that method is called you do the initial look up and populate the list that is then returned later. Then, in your utility script's Start method(), call this method to make sure it gets initialized. However, if your other script happens to start up and call this method first, you're still guaranteeing that the list gets populated.

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 1337GameDev · Jul 07, 2012 at 11:51 PM 0
Share

Seems a convoluted work around. But the script execution order menu is super useful. They need to advertise this and make it easier to get to, and easier to select scripts in a certain folder. (Right now it shows ALL scripts in assets folder, even extensions to unity or frameworks i have added)

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

7 People are following this question.

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

Unity c# Object reference not set to an instance of an Object ( Shooting Script ) 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Class c = new Class() okay in class body? 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