Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NullReferenceException : Object reference not set to an instance of an object

Discussion in 'Scripting' started by dwinaandrea, Jul 5, 2014.

  1. dwinaandrea

    dwinaandrea

    Joined:
    Apr 10, 2014
    Posts:
    3
    i am making a new 2d game using unity and C# scripting. i got an error that containt this message.
    NullReferenceException: Object reference not set to an instance of an object
    CharLayer.Update () (at Assets/CharLayer.cs:24)
    these are my codes

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CharLayer : MonoBehaviour {
    5.     public GameObject treeGO;
    6.     private TreeScript treeScript;
    7.     private float positionChar;
    8.     public const string LAYER_NAME = "ForeGround";
    9.     public const string LAYER_BACK = "Character";
    10.     public int sortingOrder = 0;
    11.     private SpriteRenderer sprites;
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         treeScript = GetComponent<TreeScript> ();
    17.         sprites = GetComponent<SpriteRenderer>();
    18.     }
    19.     // Update is called once per frame
    20.     void Update () {
    21.         positionChar = rigidbody2D.velocity.y;
    22.         if (treeScript.TreePos < positionChar)
    23.         {
    24.             sprites.sortingOrder = sortingOrder;
    25.             sprites.sortingLayerName = LAYER_NAME;
    26.         }
    27.         else if (treeScript.TreePos > rigidbody2D.velocity.y)
    28.         {
    29.             sprites.sortingOrder = sortingOrder;
    30.             sprites.sortingLayerName = LAYER_BACK;
    31.         }
    32.     }
    33. }
    and another one
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TreeScript : MonoBehaviour {
    5.     public float TreePos;
    6.     // Use this for initialization
    7.     void Start () {
    8.         TreePos = transform.localScale.y;
    9.  
    10.     }
    11.     // Update is called once per frame
    12.     void Update () {
    13.     }
    14. }
    what's wrong with my code, please help. i'm sorry, my english is so bad.
     
    Last edited: Jul 5, 2014
  2. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    If you want someone to spend time helping you with a problem, spend time using Code Tags so that what you post is more readable.

    As for your error, it means your trying to access something on an object that is missing or not linked correctly. In this case, I'd imagine one of your GetCompenent calls is falling to find something and your later trying to access it (treeScript or sprites)
     
  3. dwinaandrea

    dwinaandrea

    Joined:
    Apr 10, 2014
    Posts:
    3
    thanks NomadKing, i've edited my thread.