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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object reference not set to an instance of an object

Discussion in 'Scripting' started by coolrohit, Feb 22, 2015.

  1. coolrohit

    coolrohit

    Joined:
    Feb 22, 2015
    Posts:
    7
    i was working in project space shooter which is available in unity website .in video no 14-counting points .i got stuck .i'm getting an error on destroyByContact script .it says "Object reference not set to an instance of an object".here is my 'Destroy By Contact' Script and 'Game Controller' script.

    DestroyByContact.cs

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class DestoryByContact : MonoBehaviour {
    6.     public GameObject explosion;
    7.     public GameObject playerexplosion;
    8.     public int scorevalue;
    9.     private GameController gamecontroller;
    10.  
    11.     void start()
    12.     {
    13.  
    14.         GameObject gamecontrollerobject = GameObject.FindWithTag ("GameController");
    15.         if (gamecontrollerobject != null)
    16.         {
    17.             gamecontroller=gamecontrollerobject.GetComponent <GameController>();
    18.         }
    19.         if (gamecontroller== null)
    20.         {
    21.             Debug.Log("can.t find game controller");      
    22.         }
    23.     }
    24.  
    25.     void OnTriggerEnter( Collider other)
    26.     {
    27.         if (other.tag == "Boundary")
    28.                {
    29.             return;      
    30.                  }
    31.         Instantiate (explosion,transform.position,transform.rotation);
    32.         if (other.tag == "Player")
    33.                   {
    34.                         Instantiate (playerexplosion, other.transform.position, other.transform.rotation);
    35.                 }
    36.         gamecontroller.AddScore (scorevalue);
    37.  
    38.         Destroy (other.gameObject);
    39.         Destroy (gameObject);
    40.  
    41.     }
    42.  
    43. }
    44.  

    GameController.cs

    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class GameController : MonoBehaviour {
    7.     public GameObject hazard;
    8.     public Vector3 spawnvalues;
    9.     public int hazardcount;
    10.     public float spanwait;
    11.     public float startwait;
    12.     public float wavewait;
    13.     public GUIText scoreText;
    14.     private int score;
    15.     // Use this for initialization
    16.     void Start () {
    17.          score = 0;
    18.         updateScore ();
    19.         StartCoroutine(spawnWave ());
    20.    
    21.     }
    22.     IEnumerator spawnWave()
    23.     {
    24.         yield return new WaitForSeconds (startwait);
    25.         while (true) {
    26.                         for (int i=0; i< hazardcount; i++) {
    27.                                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnvalues.x, spawnvalues.x), spawnvalues.y, spawnvalues.z);
    28.                                 Quaternion spanrotation = Quaternion.identity;
    29.                                 Instantiate (hazard, spawnPosition, spanrotation);
    30.                                 yield return new WaitForSeconds (spanwait);
    31.                         }
    32.             yield return new WaitForSeconds (wavewait);
    33.                 }
    34.  
    35.     }
    36.     public void AddScore(int newScorevalues)
    37.     {
    38.         score += newScorevalues;
    39.         updateScore ();
    40.         }
    41.     void updateScore()
    42.     {
    43.         scoreText.text = "Score :" + score;
    44.     }
    45.  
    46. }
    47.  
    Any Help Would Be Appreciated.
    Thanks
     
  2. Oleygen

    Oleygen

    Joined:
    Feb 21, 2015
    Posts:
    24
    Which line causes an error?
     
  3. coolrohit

    coolrohit

    Joined:
    Feb 22, 2015
    Posts:
    7
    line no--36 in DestroyByContact.cs
     
  4. Oleygen

    Oleygen

    Joined:
    Feb 21, 2015
    Posts:
    24
    Did you set scoreValue in unity?
     
  5. coolrohit

    coolrohit

    Joined:
    Feb 22, 2015
    Posts:
    7
    yes
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    That means, game controller could not be initialized. You most likely got the message
    in you console
     
  7. Oleygen

    Oleygen

    Joined:
    Feb 21, 2015
    Posts:
    24
    Check this lines
    Code (csharp):
    1.  GameObject gamecontrollerobject = GameObject.FindWithTag ("GameController");
    2.   gamecontroller=gamecontrollerobject.GetComponent <GameController>();
    3.  
    Possible problem might appears if your tag have a mistake or GameController contains in other namespace, if so you have to use "using namespace_with_GameController"
     
  8. coolrohit

    coolrohit

    Joined:
    Feb 22, 2015
    Posts:
    7
    in this line i got the error message in runtime(play mode)

    Code (CSharp):
    1.    gamecontroller.AddScore (scorevalue);
    2.      
    after this line the next code
    Code (CSharp):
    1.         Destroy (other.gameObject);
    2.         Destroy (gameObject);
    3.  
    doesn't work and my tag name is fine
     
  9. Oleygen

    Oleygen

    Joined:
    Feb 21, 2015
    Posts:
    24
    Looks like you destroing your object with script and for now your gameObject == null

    And I'm not sure that I'm fully understand you. You said that error caused by this
    Which means this lines
    wont run.
    Or they are runs once, and after destroying your object you're recieving an error
     
  10. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Line 11:
    Code (csharp):
    1. void start()
    is supposed to be
    Code (csharp):
    1. void Start()
    The s has to be an uppercase character! Otherwise the start method is not executed by Unity.
     
  11. coolrohit

    coolrohit

    Joined:
    Feb 22, 2015
    Posts:
    7
    yes you were right the 's' of start must be capital.

    you just save my day.
    thanks