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:
using UnityEngine;
using System.Collections;
public class WallScript : MonoBehaviour {
public AttractedScript a;
void Start () {
a = gameObject.GetComponent<AttractedScript>();
Debug.Log (a);
}
void Update () {
}
void OnTriggerEnter(Collider other){
if (other.tag == "Cursor") {
Debug.Log ("1");
a.detect = true;
}
}
}
and this is the script that the detect boolean belongs to:
using UnityEngine;
using System.Collections;
public class AttractedScript : MonoBehaviour {
public GameObject attractedTo;
public float strengthOfAttraction = 5.0f;
public Rigidbody rb;
public bool detect;
void Start () {
detect = false;
}
void Update () {
if (detect) {
if (Input.GetMouseButtonDown (0)) {
rb.useGravity = false;
Vector3 direction = attractedTo.transform.position - transform.position;
rb.AddForce (strengthOfAttraction * direction * 20);
}
if(Input.GetMouseButtonUp(0)){
rb.useGravity = true;
detect = false;
}
}
}
}
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. This makes it impossible to grapple. Why might a be null?