Search Unity

Problem with PowerupScript

Discussion in '2D' started by Deleted User, Sep 27, 2016.

  1. Deleted User

    Deleted User

    Guest

    Hi, guys, at first i'd like to apologize for my English level :(

    So, closer to my problem. I'm using Unity for about 1 month and this is my first experience in programming.
    I'm doing a game using the :
    https://unity3d.com/ru/learn/tutorials/topics/scripting/lets-make-game-infinite-runner

    I made most of chapters, but now i stuck on one problem, which i can't remove. It lies in the fact that after i take 'coins' - they not dissapear and dont give me points. And after I am touching them, I can see this error:
    NullReferenceException: Object reference not set to an instance of an object.

    This is my PowerupScript:

    using UnityEngine;
    using System.Collections;

    public class PowerupScript : MonoBehaviour {

    HUDScript hud;

    void OnTriggerEnter2D(Collider2D other)
    {
    if(other.tag == "Player")
    {
    hud = GameObject.Find("MainCamera").GetComponent<HUDScript>();
    hud.IncreaseScore(10);
    Destroy (this.gameObject);
    }
    }

    }


    Says that problem is in line 12: hud = GameObject.Find("MainCamera").GetComponent<HUDScript>();
    Thanks to all, who will try to help me!:(
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Welcome to the forums.Please use Code Tags when pasting code snippets for better readability.

    A null reference exception means that something on that line came back null, and you then tried to access the null reference.

    You can use "Camera.main" to get the main camera more easily.

    Double check that your camera has that "HUDScript" component on it in the scene.

    You can replace that line with this to see what the problem is:

    Code (CSharp):
    1. hud = Camera.main.GetComponent<HUDScript>();
    2. if(hud == null){
    3.  
    4.     print("Camera object does not have HUDScript component");
    5.     return;
    6. }
    You probably want to get the HUDScript reference once when the object starts, and then use it OnTriggerEnter.

    You can also try this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PowerupScript : MonoBehaviour {
    5.  
    6.     HUDScript hud;
    7.  
    8.     private void Start() {
    9.         hud = Camera.main.GetComponent<HUDScript>();
    10.         if(hud == null) {
    11.             print("Could not find HUDScript on the main camera");
    12.         }
    13.     }
    14.  
    15.     private void OnTriggerEnter2D(Collider2D other) {
    16.         if(other.tag == "Player") {
    17.             if(hud != null) {
    18.                 hud.IncreaseScore(10);
    19.                 Destroy(gameObject);
    20.             }
    21.         }
    22.     }
    23. }
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    Thank you very much. I checked that my camera has HUDScript. I use yours second code. It works! I'm really happy and will try to understand the code.
    But I dont understand why this code works in the video tutorial, but not works in my project. I make all the same. This problem is becouse version Unity in video can be other than mine?
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Did you rename your camera something other than "MainCamera"? Because using GameObject.Find looks for a GameObject with the name "MainCamera", while Camera.main looks for a camera with the tag "MainCamera".

    If you want to figure out what is wrong with your original code, try breaking that line into two lines, and checking what is null like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PowerupScript : MonoBehaviour {
    5.  
    6.     HUDScript hud;
    7.  
    8.     void OnTriggerEnter2D(Collider2D other) {
    9.         if(other.tag == "Player") {
    10.  
    11.             GameObject cameraObject = GameObject.Find("MainCamera");
    12.  
    13.             if(cameraObject == null) {
    14.                 print("Could not find GameObject named 'MainCamera'");
    15.                 return;
    16.             }
    17.  
    18.             hud = cameraObject.GetComponent<HUDScript>();
    19.  
    20.             if(hud == null) {
    21.                 print("Could not find component HUDScript on the cameraObject");
    22.                 return;
    23.             }
    24.             hud.IncreaseScore(10);
    25.             Destroy(this.gameObject);
    26.         }
    27.     }
    28.  
    29. }
     
    Deleted User likes this.
  5. Deleted User

    Deleted User

    Guest

    I tried to use yours method and I found that problem was that GameObject.Find couldn't find "MainCamera", because my camera was named as "Main Camera". So, I corrected my original code and it began to work. I saved all your advices and shall be using them in my experience.

    I would like to apologize for my carelessness, because now i reviewed this tutorial video and saw that in code was "Main Camera". So, I made this error due to my inattention. But, I glad that with this I learned more helpful information. So, many thanks for your help !
     
    LiterallyJeff likes this.
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I'm glad you found the source of your issue.

    For this very reason it's best to avoid using raw strings in your code. They are prone to typos and are difficult to refactor if used in many places throughout your code. There is always a better alternative than using name strings and GameObject.Find
     
    Deleted User likes this.