• 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
1
Question by keinabel · Jan 29, 2013 at 11:14 PM · startupaddcomponentawake

Why do I have to re-add my Script on every Unity start?

Hi there,

I have an GameObject GUI. To this GO I added tw scripts: Handler and Creator.

Handler contains an multidimensional array, which is initialized in Awake(). Creator fetches Handler via GetComponent in Awake and "stores" this array into a private variable. This variable is accessed 2 times in this code. And it all works fine....

... until I close Unity. When I start it again and try to run the code, a NullReferenceException will occur at the place where I use the array mentioned above.

When I now go to GUI, remove Handler and add it again everything is fine.

But I have to do this EVERY SINGLE TIME...

Anyone got a clue what this could be? Or better, how to fix it?

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
3
Best Answer

Answer by keinabel · Jan 29, 2013 at 11:36 PM

Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth. - Unity Script Reference

The problem was caused by trying to access the array in Creators Awake(). The reference to Handler might be set in Awake(), but fetching the array at this point will cause this NullReferenceException.

I moved the getArray() call to Start() and now it works fine.

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 Bunny83 · Jan 29, 2013 at 11:43 PM 2
Share

Right, Awake is called immediately when the class get created initialized. Since Unity will create all objects one by one it can happen that when your Creator instance is created the Handler instance hasn't been created yet.

Start on the other hand is called when the whole scene is loaded. It will be called when the first frame starts, right before the first Update. At this point it's safe to access other objects.

avatar image
1

Answer by schaddemm · Jan 29, 2013 at 11:38 PM

def not normal.

It might be a bug in the unity libs (the way has the links between assets setup). I think RebuildAllAssets could fix that. Also, just in case, Save the scene after making your change. Also, check that the modification to your GUI-gameobject is applied to your prefab. (click apply in the inspector)

Other thing I could imagine is that its something script related, bad initialization code.

Just reread your post, sounds like bad initialization code. Do you have the link=GetComponent() in Start or Awake? Maybe you're accessing the component before it's been created, which would explain the nullreference.

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 keinabel · Jan 29, 2013 at 11:45 PM 0
Share

It was bad initialization. I'm still quite the unity newby.

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

11 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

Related Questions

AddComponent passing variable before Start/Awake 5 Answers

Attach a script to GameObject in a DLL 0 Answers

ExecuteInEditMode Component construct "twice" and "reset" custom classes 0 Answers

GO.AddComponent just adds an empty Script 1 Answer

Copy a script from one gameobject to another? 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