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 MurderousPotato · Aug 27, 2016 at 06:51 AM · nullreferenceexceptionarraysnullissuesclass object

Object is null when it shouldn't be.

So I'm working on something to create an array to store the data for locations you can interact with on a map, and I'm using an object called landmark which houses all relevant data needed when the game is saved and loaded.

The problem is that the object itself is recognized as null, which has made it impossible for me to store in an array (a NullReferenceException pops up every time).

Here's some example code, hopefully someone can figure out whatever mistake I'm making.

 //an example of the landmark class
 
 public class landmark : MonoBehaviour {
 
 public int sectorNumber;
 public int landmarkID;
 public float positionX;
 public float positionY;
 
 }
 
 //a test script showing the issue
 
 public class landmarkTest : MonoBehaviour {
 
 landmark[] testArray;
 landmark landmarkTest;
 
 void Start()
     {
         testArray = new landmark[1];
         landmarkTest = new landmark();
        
         testArray[0] = landmarkTest;
         print(testArray[0]);
     }
 //when the print command runs, it prints "null" to the console.
 //however, when I print testArray, it prints "landmark[]"
 
 }
 

Note: In case you couldn't tell, I'm a beginner to both C# and Unity so the answer might be blindingly obvious.

Comment
Add comment · Show 2
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 mj321 · Aug 27, 2016 at 09:00 AM 0
Share

I'm a beginner with both Unity and C#, so i'm not sure. But i think with the line "testArray = new landmark[1];" you only create an array, but it doesn't automatically fill it with landmark instances. Try adding "testArray[0] = new landmark();" after that line.

avatar image Bunny83 mj321 · Aug 27, 2016 at 10:15 AM 0
Share

He actually does that. The problem is, like $$anonymous$$Watts said, you must not create a $$anonymous$$onoBehaviour with "new". $$anonymous$$onoBehaviours are "created" with AddComponent or by attaching them inside the editor to a gameobject.

1 Reply

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

Answer by DavidWatts · Aug 27, 2016 at 09:07 AM

landmark should derive from MonoBehaviour if you only need it to store data. If a class derives from MonoBehaviour it needs to be attached to a game object and created with AddComponent instead of new. so just take out the MonoBehaviour so it reads

 public class landmark {

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can this possibly be null? 2 Answers

reference an object that is yet to be in the scene 1 Answer

Why Again Arrays In ExecuteInEditMode() Gives NullReferenceException Errors?? 1 Answer

NullReferenceException: Object reference not set to an instance of an object spawn.Update () (at Assets/spawn.cs:24) 1 Answer

Attempt at creating an array of text files results in different errors 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