- Home /
Question by
MH1 · Apr 20, 2014 at 01:56 PM ·
c#arraynullreferenceexceptiondebug
NullReferenceException: Object reference not set to an instance of an object player.Update () (at Assets/player.cs:10)
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
private int[] coinupto;
void Start () {
coinupto = new int[10];
coinupto[1]=8;
}
void Update () {
Debug.Log(coinupto[1]); //the line throwing the error
}
}
Comment
Answer by fendorio · Apr 20, 2014 at 02:37 PM
Where is mainTrigger? It's never assigned to in your code snippet so that would be null.
You also haven't instantiated the array in your code snipped so assigning a value to an index is invalid.
private int[] coinupto;
void Start()
{
//Instantiate it first.
coinupto = new int[10]; //Replace 10 with how many values you want to store.
coinupto[1] = 8;
}
I don't think that's what you're trying to do though? Your code is confusing. You need to add a GeComponent to your script to fetch the maintriggerscript.
If maintriggerscript is attached to the same gameObject as the one that holds player:
maintriggerscript SomeScript = GetComponent("maintriggerscript");
Else if it's in another gameObject:
maintriggerscript SomeScript = GameObject.FindGameObectWithTag("sometag")
.GetComponent("maintriggerscript");
Sorry, I didn't realize that I left the reference to maintriggerscript in my code. I took it out and still got the same error. I updated my script.
In your start function have you added the line:
coinupto = new int[10];
That is, before/above the line:
coinupto[1] = 8;