- Home /
Problem is not reproducible or outdated
Null Reference Expantion
Hello there, i have two kind of scripts. One is board another is animation, if i play my animation seperately its working fine. if i call that animation to board script, it says null reference expantion, i cant clearly with words so i will add my scripts here.
Animation Coding:
using UnityEngine; using System.Collections;
public class AnimationObject : MonoBehaviour {
public bool showButton = false;
void start()
{
}
public void OnGUI()
{
if(showButton)
{
if(GUI.Button(new Rect(50,30,80,30),"Dice Roll"))
{
animation.Play("Take 001");
}
}
}
public void Update()
{
showButton = true;
}
}
Board Coding:
using UnityEngine; using System.Collections;
[ExecuteInEditMode] [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] [RequireComponent(typeof(MeshCollider))]
public class Board : MonoBehaviour { public int size_x=10; public int size_z=10; public float tileSize=1.0f; public AnimationObject getFunction;
// Use this for initialization
void Start ()
{
CreateBoard();
}
public void CreateBoard()
{
int numTiles = size_x * size_z;
int numTris = numTiles * 2;
int vsize_x = size_x + 1;
int vsize_z = size_z + 1;
int numVerts = vsize_x * vsize_z;
Vector3[] vertices = new Vector3[numVerts];
Vector3[] normals = new Vector3[numVerts];
Vector2[] uv = new Vector2[numVerts];
int[] triangles = new int[numTris*3];
int x,z;
for(z=0; z < vsize_z; z++)
{
for(x=0 ; x < vsize_x; x++)
{
vertices[ z * vsize_x + x]=new Vector3(x * tileSize, 0 , z*tileSize);
normals[z * vsize_x + x]=Vector3.up;
uv[z * vsize_x + x]=new Vector2((float)x / size_x,(float)z / size_z);
}
}
for(z=0; z < size_z; z++)
{
for(x=0 ;x < size_x; x++)
{
int squareIndex = z * size_x + x;
int trioffset = squareIndex * 6;
triangles[trioffset + 0] = z * vsize_x + x + 0;
triangles[trioffset + 2] = z * vsize_x + x + vsize_x + 1;
triangles[trioffset + 1] = z * vsize_x + x + vsize_x + 0;
triangles[trioffset + 3] = z * vsize_x + x + 0;
triangles[trioffset + 5] = z * vsize_x + x + 1;
triangles[trioffset + 4] = z * vsize_x + x + vsize_x + 1;
}
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uv;
MeshFilter mesh_filter = GetComponent<MeshFilter>();
MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
MeshCollider mesh_collider = GetComponent<MeshCollider>();
mesh_collider.sharedMesh = mesh;
mesh_filter.mesh = mesh;
}
// Update is called once per frame
void Update ()
{
getFunction.OnGUI();
getFunction.Update();
}
}
Answer by lexvandersluijs · Feb 14, 2014 at 09:38 AM
Hey Prasanna,
You are using ExecuteInEditMode which will call the Update function as soon as you assign the Board script to your GameObject. And at that moment you probably have not set you AnimationObject yet. which will result in a null reference exception. Try modifying your update function to the following:
void Update()
{
if( getFunction != null )
{
getFunction.OnGUI();
getFunction.Update();
}
}
Hi lexvanderslujis, i tried your code it says error. "error CS1525: Unexpected symbol `!'"