Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 04, 2014 at 04:53 AM by Prasanna for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Prasanna · Feb 14, 2014 at 08:47 AM · errornullreferenceexceptionfunction

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();
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

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();
   }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Prasanna · Feb 14, 2014 at 10:39 AM 0
Share

Hi lexvanderslujis, i tried your code it says error. "error CS1525: Unexpected symbol `!'"

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to call a function from another script - getting error "Object reference not set to an instance of an object" 5 Answers

C# custom class array error in adding a new entry. 1 Answer

Null Reference exception help 2 Answers

GameManager trying to access method from deleted GameManager via the InvokeRepeating() function 0 Answers

Strange errors with GUI.skin 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges