Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 donpek1 · May 31, 2015 at 08:23 PM · arrayinspectornullreferenceexceptionreferenceinitialization

Array values not being initialised unless the object with the (Player) script is selected in the inspector whilst the game is on

... Otherwise, you just get a "NullReferenceException: Object reference not set to an instance of an object"

Full error: "NullReferenceException: Object reference not set to an instance of an object Player.SetRelationship (Int32 currentLog, System.String npcLabel) (at Assets/Scripts/Player.js:52) SpeakMod.Update () (at Assets/Scripts/SpeakMod.js:76)"

The code: (Player.js)

 var movementSpeed : float = 1;
 var canMove : boolean = true;
 var numOfNPCs : int;
 
 
 public class Relationship
 {
     var log : int;
     var npc : String;
     
     function Relationship(log : int, npc : String)
     {
         this.log = log;
         this.npc = npc;
     }
 }
 var relationships : Relationship[];
 private var tempNum : int;
 
 function Start()
 {
     relationships = new Relationship[numOfNPCs];
 }
 
 function FixedUpdate () {
     if(canMove)
     {
         if(Input.GetKey(KeyCode.A))//-MOVEMENT--------------
         {
             transform.position.x -= movementSpeed * 0.1;
         }else if(Input.GetKey(KeyCode.D)){
             transform.position.x += movementSpeed * 0.1;
         }
 
         if(Input.GetKey(KeyCode.S))
         {
             transform.position.y -= movementSpeed * 0.1;
         }else if(Input.GetKey(KeyCode.W)){
             transform.position.y += movementSpeed * 0.1;
         }//-MOVEMENT-----------------------------------------
     }
 }
 
 function SetRelationship(currentLog : int, npcLabel : String)
 {        
     for(var x = 0; x < relationships.length; x++)
     {
         if(relationships[x].npc == npcLabel)
         {
             relationships[x].log = currentLog;
             return;
         }
     }
     var newRelationship = new Relationship(currentLog, npcLabel); 
     relationships[tempNum] = newRelationship;
     tempNum++;
 }


(SpeakMod)(the part that matters)-------------------------------------------------------

 function Update()
 {
     if(Input.GetKeyUp(KeyCode.Space) && triggered)
     {    
         askee.GetComponentInParent(Player).canMove = false;
         speechWindow.SetActive(true);
 
         
         if(logs[logCounter].conditions.name && askee.GetComponentInChildren(Name).gameObject.GetComponent(TextMesh).text == 
         logs[logCounter].conditions.requiredName) logs[logCounter].conditions.name = false;
         
         
         if(logs[logCounter].conditions.speakWithXAboutY)
         {
         
             for(var i = 0; i < askee.GetComponentInParent(Player).relationships.length; i++)
             {
                 if(logs[logCounter].conditions.withX.GetComponent(SpeakMod).myLabel == askee.GetComponentInParent(Player).relationships[i].npc &&
                 logs[logCounter].conditions.aboutY_Log == askee.GetComponentInParent(Player).relationships[i].log) logs[logCounter].conditions.speakWithXAboutY = true;
             }
         }
         
         
 
         if(!logs[logCounter].conditions.name && !logs[logCounter].conditions.item 
         && !logs[logCounter].conditions.speakWithXAboutY && !logs[logCounter].conditions.noConditions) logs[logCounter].conditions.conditionsMet = true;
         
         if(pageCounter == logs[logCounter].pages.length - 1)
         {
             askee.GetComponentInParent(Player).SetRelationship(logCounter, myLabel);

The lines in questions are:

Player.js:52 "relationships[x].log = currentLog;"

and

SpeakMod.js:76 "askee.GetComponentInParent(Player).SetRelationship(logCounter, myLabel);"

I've never encountered this kind of bug - no idea how to fix it. :/

Thanks in advance, ~Donatas.

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 Graham-Dunnett · May 31, 2015 at 08:37 PM

As you'll know, NullReferenceException means that you are accessing a variable that doesn't exist. Since in your case you are accessing an Array, I'd imagine that the array hasn't been created. So perhaps use Debug.Log to work out what's happening after line Player.js:22. Does that array have the size you expect? Also, at line 55, what's tempNum set to? I can't see it given a value anywhere. Maybe you are trying to access an Array element that's too big for the array.

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

2 People are following this question.

avatar image avatar image

Related Questions

Custom inspector, a multidimensional array of enums 1 Answer

NullReferenceException with Javascript Array 2 Answers

C# custom class array error in adding a new entry. 1 Answer

Why can't I access the Name field in the Custom Styles section of a GUISkin in the Inspector? 2 Answers

Arrays don't show up on inspector 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