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

What is the meaning of this error ?

Discussion in 'Scripting' started by m-y, May 30, 2014.

  1. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    $what.png
     
    Last edited: May 30, 2014
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,935
    Can you show the code from line 35? (Garab.cs)
     
  3. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    that is the line number 35
    gameController.AddScore (scoreValue) ;

    AddScore is a method of another script

    and ( Scorevalue )
    is an integer
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,935
    Have you assigned gameController somewhere?
     
  5. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470

    ok that is the code of script ( Garab )

    using UnityEngine;
    using System.Collections;

    public class Garab : MonoBehaviour {



    public int scoreValue;
    private Done_GameController gameController;

    void Start ()
    {
    GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
    if (gameControllerObject != null)
    {
    gameController = gameControllerObject.GetComponent <Done_GameController>();
    }
    if (gameController == null)
    {
    Debug.Log ("Cannot find 'GameController' script");
    }
    }

    void OnTriggerEnter (Collider other)
    {
    if (other.tag == "Enemy" )
    {
    return;
    }





    gameController.AddScore (scoreValue) ;
    Destroy (other.gameObject);
    Destroy (gameObject);
    }
    }


    as you can see i called another script called Done_GameController then i create an object from it
    to use it for adding score
    but this script ( Garab )
    make alot of problem in the game

    and the error on the picture u saw in above i dont understand the meaning of it !
     
  6. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    A null reference exception means you are attempting to access memory with nothing in it.

    Code (csharp):
    1.  
    2. MyObject newObject;
    3. MyObject.ToString();
    4.  
    This will throw a null reference exception because newObject was never given a value. The fix for the code would be like this.

    Code (csharp):
    1.  
    2. MyObject newObject = new MyObject();
    3. MyObject.ToString();
    4.  
    The ToString method now has an actual object to reference and thus no longer throws an error. In unity newObject can be set in another way, by setting the variable to public and dragging a reference into the inspector slot. Somehow in your code a variable is not being set to a value correctly.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Garab : MonoBehaviour {
    6. public int scoreValue;
    7. private Done_GameController gameController;
    8.  
    9. void Start () {
    10.    GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
    11.    if (gameControllerObject != null) {
    12.       gameController = gameControllerObject.GetComponent <Done_GameController>();
    13.    }
    14.    if (gameController == null) {
    15.       Debug.Log ("Cannot find 'GameController' script");
    16.    }
    17. }
    18.  
    19. void OnTriggerEnter (Collider other) {
    20.       if (other.tag == "Enemy" ) {
    21.          return;
    22.       }
    23.       gameController.AddScore (scoreValue) ;
    24.       Destroy (other.gameObject);
    25.       Destroy (gameObject);
    26.    }
    27. }
    28.