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
1
Question by mcabezas26 · Sep 28, 2015 at 06:38 AM · beginnernullreferenceexception

NullReferenceException even though I assigned it?

Hi, I'm a newbie when it comes to Unity, so forgive me if the answer to this is brutally obvious.

I'm making a Gravity controller using the following code:

In a script called GlobalPhysics, where I store all the global variables, I have a float variable. called Gravity. I declared it as " public float Gravity = 9.8f;".

In a separate test script attached to the game object I want to manipulate, I put in this line to put the gravity in effect:

     public Rigidbody rbTest;
     private GlobalPhysics globalPhysics;
     
     void FixedUpdate()
     {
         rbTest.AddForce(Vector3.down * globalPhysics.Gravity * rbTest.mass);
     }


However, when I go and run the script, it keeps throwing the error "NullReferenceException : Object reference not set to an instance of an object". I've looked at several questions and answers and I haven't been able to find much. How do I fix this?

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 cjdev · Sep 28, 2015 at 07:50 AM

The problem you have is that you are declaring the globalPhysics variable of type GlobalPhysics (your script) but you aren't actually creating a GlobalPhysics object to store in that variable. Normally you'd need to do something like this:

 void Start() {
     globalPhysics = new GlobalPhysics();
 }

That way you have an object that you can use to get the variable Gravity it contains. However, if you are looking to make a class purely of global variables I would definitely recommend using a static class. What this means is that you don't need to make an object like this in order to access the variables and functions of the class, which makes it work well with constants and global variables. To make a static class you'd do something like this:

 public static class GlobalPhysics {
 
     public static float gravity = 9.81f;
 
 }

And then you could call it in your script like this:

 rbTest.AddForce(Vector3.down * GlobalPhysics.gravity * rbTest.mass);

Notice that there's no instantiating or new variables required, which makes it a lot more flexible.

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 NicknEmart · Aug 21, 2020 at 12:20 PM 0
Share

@cjdev I am having the same issue and creating a new object actually makes things worse. In addition to the NullReferenceException now I get a warning that says "you are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed. $$anonymous$$onoBehaviours can only be added using AddComponent()." What am I doing wrong?

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

NullException Error With Array of GameObjects 2 Answers

Not sure why I keep getting a Null Reference Exception? (Networking) 0 Answers

Assignment working in Update() but not in Start()? 1 Answer

Passing Values/Variables between Classes 2 Answers

Problem for moving an object with a button 0 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