• 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 Tronnyi · Oct 04, 2015 at 11:16 AM · arraynullreferenceexception

Array NullReferenceException

So, i didnt really find anything that helped me in other threads, so here is myproblem: I want to generate random Quests for my game so i created one script that is not on an object containing the following code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
     
 [System.Serializable]
 
 public class QuestsType : System.Object
 {
     public string questName;
     public string giverName;
     public int type;
     public int difficulty;
     public int questLength;
 }
 

and then i have this second script, wich i hoped would give me the value of the variable "type" of each of the generated arrays (just for testing):

     public int questAmountMin;
     public int questAmountMax;

     public QuestsType[] quests;
 
     void Start ()
     {
         GenerateQuests();
     }
 
     void GenerateQuests()
     {
         quests = new QuestsType[Random.Range(questAmountMin,questAmountMax)];
         for (int i = 0;  i < quests.Length ; i++)
         {
             print(quests[i].type);
         }
     }

It will then always (except if the random "quests" array has a length of 0) give me a NullReferenceException: Object reference not set to an instance of an object - error. The strange thing now is that it acually works, but only if i manually execute the

         for (int i = 0;  i < quests.Length ; i++)
         {
             print(quests[i].type);
         }

-portion via a button or something, and ONLY IF the inspector shows the Gameobject with the second script. ive never seen something like that before..

I would really appreciate your help!

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

Answer by Pharan · Oct 04, 2015 at 11:40 AM

Instantiating the array is a different step from instantiating the objects inside the array.

Because QuestsType is a class, the array QuestsType[] quests is an array of references to QuestsType. And all references are initialized by default as null references (references that point to nothing) until you create something it can point to.

When you first create the array (line 13), it's an array of references to nothing.

You need to go:


    for (int i = 0; i < quests.Length; i++)
    {
         quests[i] = new QuestsType();
         // then you can do this
         print(quests[i].type);
    }

There's also no need for QuestsType to be declared as QuestsType : System.Object. All declared classes already inherit from System.Object without you writing anything on there.

What programming language did you come from? So we can point you in more helpful directions.

Comment
Add comment · Show 2 · 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 Tronnyi · Oct 04, 2015 at 11:50 AM 0
Share

Thank you very much! That seems to work. I am learning it by myself and are still very much a scrub, using the internet and those forums a lot, also im co$$anonymous$$g from JS.

avatar image Pharan Tronnyi · Oct 04, 2015 at 03:01 PM 0
Share

Glad to help. It's cool that you're jumping straight into C#. C# code samples and documentation are easier to find.

Unity's managed/scripting runtime is $$anonymous$$ono/.NET. It's just one generation ahead of Java, and takes most its paradigms from it (class-based OOP that's way different from javascript's prototype-centric OOP, abstraction of pointer operations, single-inheritance, managed memory/garbage collection, exceptions) but improves on it with the aim of cleaner code in C# (value-type structs, get-set properties and auto-properties, events, delegates and lambdas, extension methods). Anyway, point is that you'll find more weird stuff if you come from a javascript perspective.

The basic topic you ran into was "the difference between reference types and value types". You can look that up if you want to get a solid grasp of it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Populating a Vector2 array in Javascript 1 Answer

List or Array for Rays 1 Answer

NullReferenceException with an Array 2 Answers

Why is passing this array causing some of its values to become null? 2 Answers

[Fixed] Editing an array from another class (nullReferenceError) 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