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*.npc &&*

logs[logCounter].conditions.aboutY_Log == askee.GetComponentInParent(Player).relationships*.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. :confused:
Thanks in advance,
~Donatas.

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.