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

Unexpected NullReferenceExecption

Discussion in 'Editor & General Support' started by CapitalistSquid, Sep 12, 2015.

  1. CapitalistSquid

    CapitalistSquid

    Joined:
    Aug 30, 2015
    Posts:
    12
    I am attempting to create a 2.5d platformer where you can use a grappling hook to move around. The grappling hook is aimed by the cursor. I am trying to make it so you can only grapple onto certain walls. This script is attached to the walls that can be grappled onto:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WallScript : MonoBehaviour {
    5.  
    6.     public AttractedScript a;
    7.     void Start () {
    8.         a = gameObject.GetComponent<AttractedScript>();
    9.         Debug.Log (a);
    10.     }
    11.    
    12.     void Update () {
    13.        
    14.     }
    15.    
    16.     void OnTriggerEnter(Collider other){
    17.         if (other.tag == "Cursor") {
    18.             Debug.Log ("1");
    19.             a.detect = true;
    20.         }
    21.     }
    22. }
    23.  
    and this is the script that the detect boolean belongs to:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AttractedScript : MonoBehaviour {
    5.     public GameObject attractedTo;
    6.     public float strengthOfAttraction = 5.0f;
    7.     public Rigidbody rb;
    8.     public bool detect;
    9.    
    10.     void Start () {
    11.         detect = false;
    12.     }
    13.    
    14.     void Update () {
    15.         if (detect) {
    16.             if (Input.GetMouseButtonDown (0)) {
    17.                 rb.useGravity = false;
    18.                 Vector3 direction = attractedTo.transform.position - transform.position;
    19.                 rb.AddForce (strengthOfAttraction * direction * 20);
    20.             }
    21.  
    22.             if(Input.GetMouseButtonUp(0)){
    23.                 rb.useGravity = true;
    24.                 detect = false;
    25.             }
    26.         }
    27.     }
    28.    
    29. }
    For some reason, I recive this error "
    NullReferenceException: Object reference not set to an instance of an object
    WallScript.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/WallScript.cs:19)"
    It surprises me that a is always null despite being assigned gameObject.GetComponent<AttractedScript>. This makes it impossible to grapple. Why might a be null?
     
  2. goonter

    goonter

    Joined:
    Aug 31, 2015
    Posts:
    89
    Unless AttractedScript and WallScript are on the same gameobject, that won't work. gameObject.GetComponent gets the script from the current gameobject, so it's looking for AttractedScript on your wall, which probably isn't what you want.
     
  3. CapitalistSquid

    CapitalistSquid

    Joined:
    Aug 30, 2015
    Posts:
    12
    Goonter, thank you for telling me this. What command would allow me to get the script from a different gameobject?
     
  4. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287