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){

        _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.

  • \$\begingroup\$ Where do you initialize the meshes? In the editor? \$\endgroup\$ – user15805 Mar 15 '13 at 19:28
  • \$\begingroup\$ Yes in the inspector i dragged and dropped every .obj frame i need in the Meshes[] array. \$\endgroup\$ – CannonBall Mar 15 '13 at 19:31
  • 1
    \$\begingroup\$ Does your gameobject have an actual MeshFilter component added to it? \$\endgroup\$ – user15805 Mar 15 '13 at 19:37
  • 2
    \$\begingroup\$ Voting to close as "Too localized." The purpose of this site isn't best used for helping figure out small errors like a NullReferenceException. \$\endgroup\$ – michael.bartnett Mar 15 '13 at 19:38
  • \$\begingroup\$ 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. \$\endgroup\$ – 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:


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.

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.