Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NPS · Jun 30, 2014 at 07:48 AM · monobehaviourerror-message

Exposed variable is null in Awake

I have an exposed variable that I attach to in the Inspector. But when I run the game Unity throws a NullReferenceException in the script's Awake() function. And - yes, I am sure the variable is attached. Also, I've tried Start() instead and the same happens.

Even more weird, after Awake() (i.e. Update()) the variable is set properly and the whole script/game seems to be running fine, like there was never any error. Yet one is thrown.

Any ideas what might be the problem and how to fix it? I don't feel comfortable leaving an error in my game.

Track.cs:

 using System.Collections.Generic;
 using UnityEngine;
 
 public class Track : MonoBehaviour
 {
     public MegaShape envCrossSectionCCW;
 
     [HideInInspector]
     public MegaShape envCrossSectionCW;
 
     void Awake()
     {
         var cw = (GameObject)Instantiate(envCrossSectionCCW.gameObject);   // NullReferenceException
         cw.transform.parent = envCrossSectionCCW.transform.parent;
         cw.name = "EnvCrossSectionCW";
         envCrossSectionCW = cw.GetComponent<MegaShape>();
     }
 }

Edit: When I try to debug the code the debugger stops at the commented line and when I check the value of envCrossSectionCCW it is null. However, when I try to proceed to the next line of code in the debugger it jumps... again to the same line of code (like it resetted or sth) and this time envCrossSectionCCW isn't null anymore. From this point the debugging proceeds without a problem.

EDIT: Today I've discovered that my Awake() is called twice(!) for some reason (that debugger behaviour was just that - it went through the code twice, first time ending in an error). The GameObject with Track script is stores in Resources and instantiated like that:

  (GameObject)Instantiate(Resources.Load("TrackMountainsForest"));

I'm pretty sure I have only 1 object with this script attached in the entire scene. Any idea why Awake() gets called twice?

Comment
Add comment · Show 8
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 Denvery · Jun 30, 2014 at 07:52 AM 1
Share

Please post your script

avatar image Denvery · Jun 30, 2014 at 11:26 AM 0
Share

Thanks! And which line cases the NullReferenceException? Which variable does not set properly?

avatar image RiQQ · Jun 30, 2014 at 11:45 AM 0
Share

I'm not 100% sure, but are you definite that there is parent in all of the objects, if everything works sounds like no parent is found and null reference is thrown?

avatar image NPS · Jun 30, 2014 at 12:10 PM 0
Share

I've commented the line that throws the exception. envCrossSectionCCW is null. Also, envCrossSectionCCW has a parent.

avatar image NPS · Jun 30, 2014 at 12:17 PM 0
Share

I've also added a description of what happens when I try to debug the code. I'm not sure if the debugger can be trusted here (I'm using Visual Studio 2012) but this is what it does.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by brunopava · Jun 30, 2014 at 11:46 AM

The var keyword is used on Javascript, to declare variables with C# you simply state the type of it.

 //Your code
 var cw = (GameObject)Instantiate(envCrossSectionCCW.gameObject);
 
 //My code
 GameObject cw = GameObject.Instantiate(envCrossSectionCCW.gameObject, 
                                        positionVector, 
                                        rotationVector) as GameObject;

Comment
Add comment · Show 5 · 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 Denvery · Jun 30, 2014 at 11:48 AM 0
Share

Var in c# and in this code is absolutely legal: http://msdn.microsoft.com/ru-ru/library/bb383973.aspx

avatar image NPS · Jun 30, 2014 at 12:12 PM 1
Share

What Denvery said + just to be sure I've checked - specifying the type explicitly doesn't make the error go away.

avatar image hackingdandan NPS · Jan 02, 2021 at 06:09 PM 0
Share

yeah C# has allowed inferred variable declaration since c# 3.

Only exception is if you're just declaring the name of the variable to instantiate it later.

 GameObject myThing;
 
 myThing = GameObject.Instantiate(...);

*edited

avatar image Bunny83 hackingdandan · Jan 02, 2021 at 06:37 PM 0
Share

"var" is not a dynamic variable. It's not dynamic as in "it can change its type dynamically". It's just a type inferred variable declaration. Implicitly typed variables can only be declared inside methods and not inside class / type declarations.

A dynamic variable in C# would be a variable of type "object". Besides that there is the type "dynamic". However it comes with runtime overhead and generally makes code harder to read and debug-


You did realise that this question and comment is over 6 years old, right?

Show more comments

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

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

Related Questions

Issue with scripts, error code "script needs to derive from monobehaviour" 0 Answers

Problem with attaching scripts to objects 1 Answer

How do I detect that MonoBehaviour is modified in the Editor? 2 Answers

Unit testing for MonoBehaviours 2 Answers

Why does the monobehaviour name have the _mono_ prefix? 2 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