Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Nacho84 · Sep 28, 2011 at 07:23 PM · updatestart

Script initialization when instancing a game object

Hi there! I have a problem here with my code which is making me scratch my head, so I've decided to come here and ask for help: I have a prefab composed of several game objects. One of them has 4 C# script objects attached (A, B, C and D).

Script A initializes its variables inside its own Start method. In the Update function, it occasionally calls method X located inside Script B.

Script B also initializes its variables inside its own Start method, which are needed in order to properly execute method X.

Now, for some reason Script A is calling method ScriptB::X before Script::Start has even executed, which causes a null reference exception.

Now I've checked the documentation and I think I'm not doing anything illegal here. Can anyone help me with this?

Thanks in advance!

--Nacho

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 Bovine · Sep 28, 2011 at 07:33 PM 0
Share

Without seeing the scripts it is had to say. So long as your method X isn't being called in start. Here are some debugging suggestions:

  1. Log that you have exited each Start() method

  2. Log that you have entered each Update()

  3. Walk through the code in mono's debugger to see that Yes, script A starts up ad Yes so does script B

  4. If anything in either script is set to null, then log when this occurs (and maybe log when it's instantiated).

  5. Check the Console for when your null exception is occurring and you should see what happened around it - did your Start() methods succeed?

Off the top of my head I wonder if:

  • Your reference to script B in A and visa versa are null rather than the objects in side method X?

  • Whether one of your scripts is attached to a GO that is not active, in which case Start() won't be called for your script and everything really WILL be null - adding some logging as above may point to this sort of thing.

Cheers H

avatar image Nacho84 · Sep 28, 2011 at 07:49 PM 0
Share

Thanks for your reply! I logged in that information and I can confirm that things occur in the following order: 1) Script A's Start method is properly executed. 2) Script A's Update method is executed (which calls method X from Script B and causes a null reference exception). 3) Script B's Start method is properly executed.

Both scripts are attached to the same object, so I don't get why is Script B's Start method executed after Script A's Update method and not before. Let me know if you need further information.

Thanks!

avatar image Bovine · Sep 28, 2011 at 07:55 PM 1
Share

That seems very odd. You should check out the execution order of the scripts in question:

Edit -> Project Settings -> Execution Order

But I confess I thought all Start() methods occurred before Updates()...

As another suggestion, you might want to set things up in Awake(). This would be a great place to "make" things, just don't expect any other scripts to be Awake() yet, though in my experience it is fine to GetComponent() on the GO for the active script so you can store things like the Transform etc.. locally.

avatar image Nacho84 · Sep 28, 2011 at 08:11 PM 0
Share

Once again, thanks for your reply! I haven't changed the scripts execution order (besides, the scripts are inside a game object which belongs to a prefab that is instantiated at run-time).

In any case, setting things up in Awake made the trick, so I can move on!

Thanks again for your help!

avatar image BerggreenDK · Sep 28, 2011 at 08:56 PM 0
Share

Also check if you could use Awake ins$$anonymous$$d of Start, as Awake comes before Start. I think Start is when the objects are in the hierachy, where as Awake is when the constructor is called?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

Spawning different objects 1 Answer

Deactivate Update() Function. 1 Answer

A force is applied to a stationary rigid body. Velocity before and after application of force is printed but both values are shown as zero. Why? 1 Answer

FindGameObjectWithTag wont find in Start() or Awake() but will in Update() 1 Answer

Use my own update() and start() methods. 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