# unity3d nullreferenceexception [closed]

i want to display a mesh sequence so i imported my obj files and found a script and partially edit it a bit.My project looks like this:

the script is this:

using UnityEngine; using System.Collections;

/** * Animate a mesh by cycling through different meshes. * @author bummzack */ public class MeshAnimation : MonoBehaviour {

public Mesh[] Meshes;
public bool Loop;
public float FrameDuration;

private int _index;
private bool _playing = true;
private float _accumulator;
private MeshFilter _meshFilter;

public void Start()
{
_meshFilter = GetComponent<MeshFilter>();
_index = 0;
}

public void Update()
{
_accumulator += Time.deltaTime;

if(_accumulator >= FrameDuration){
_accumulator -= FrameDuration;
_index = (_index + 1) % Meshes.Length;

if(_index == 0 && !Loop){
Stop();
return;
}

_meshFilter.mesh = Meshes [_index];
print ("Hello World");
}
}


}

the line that causes the problem is this:

_meshFilter.mesh = Meshes[_index];

and the problem shown in the console is this:

NullReferenceException MeshAnimation.Update () (at Assets/MeshAnimation.cs:43)

i know it has something to do with Meshes[_index} being null but i cant find the solution because i think the Meshes array is properly filled.Can anyone suggest a solution? Thank you all in advance.

## closed as too localized by michael.bartnett, MichaelHouse♦, TetradMar 15 '13 at 21:08

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

• Where do you initialize the meshes? In the editor? – user15805 Mar 15 '13 at 19:28
• Yes in the inspector i dragged and dropped every .obj frame i need in the Meshes[] array. – CannonBall Mar 15 '13 at 19:31
• Does your gameobject have an actual MeshFilter component added to it? – user15805 Mar 15 '13 at 19:37
• Voting to close as "Too localized." The purpose of this site isn't best used for helping figure out small errors like a NullReferenceException. – michael.bartnett Mar 15 '13 at 19:38
• Also, Bogdan is on the right track--it's highly likely that your MeshFilter is null because public arrays on MonoBehaviours such as Meshes basically always get assigned a reference at runtime, even if the length is 0. – michael.bartnett Mar 15 '13 at 19:41

michael.bartnett made a good point in the comments, and the only object left to put the blame on is the MeshFilter. Be sure one is added via the editor as a gameobject component, otherwise GetComponent<MeshFilter>() will return null.

Extra piece of advice! Do this for every GetComponent<T> call:

SomeComponent = GetComponent<SomeType>();
if (SomeComponent == null)
{
Debug.LogError("ERROR! Unable to get component in GameObject: " + this.name);
}


This will log an error message in the console every time a component is not found. It will help you a lot with debugging.

Extra piece of advice #2! You can use the [RequireComponent (typeof (YourComponent))] attribute to make things easier. Take a look at this:

http://docs.unity3d.com/Documentation/ScriptReference/RequireComponent.html

In short, setting [RequireComponent (typeof (MeshFilter))] in your script will force Unity to auto-add a MeshFilter component to any GameObject to which you add your script.