0

I'm getting NullReferenceException and can't figure out what I am supposed to be fixing. I've read through the manuals for NRE's and raycasting but I can't find any answer.

NullReferenceException
UnityEngine.Collider.Internal_Raycast (UnityEngine.Collider col, Ray ray, UnityEngine.RaycastHit& hitInfo, Single maxDistance)
(at C:/buildslave/unity/build/artifacts/generated/common/modules/DynamicsBindings.gen.cs:2257)
UnityEngine.Collider.Raycast (Ray ray, UnityEngine.RaycastHit& hitInfo, Single maxDistance)
(at C:/buildslave/unity/build/artifacts/generated/common/modules/DynamicsBindings.gen.cs:2265)
TileMouseOver.Update () (at Assets/TileMouseOver.cs:22)

using UnityEngine;
using System.Collections;

public class TileMouseOver : MonoBehaviour {
  [SerializeField]
  public Color highlightColor;
  [SerializeField]
  Color normalColor;

  void Start() {
    normalColor = GetComponent<Renderer>().material.color;
  }

  void Update () {
    Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
    var rayCollider = new Collider();
    RaycastHit hitInfo;

    if( rayCollider.Raycast( ray, out hitInfo, Mathf.Infinity )) {
        GetComponent<Renderer>().material.color = highlightColor;
    } else {
        GetComponent<Renderer>().material.color = normalColor;
    }
  }
}
Glorin Oakenfoot
  • 2,105
  • 15
  • 19
Thusten
  • 11
  • 3

1 Answers1

0

Haven't dealt directly yet but from what I'm seeing you're creating a collider where you should be using a collider that's already attached to the gameObject.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
public Collider coll;
void Start() {
    coll = GetComponent<Collider>();
}
void Update() {
    if (Input.GetMouseButtonDown(0)) {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (coll.Raycast(ray, out hit, 100.0F))
            transform.position = ray.GetPoint(100.0F);

    }
}

}

In the Start method you can see that the code is grabbing a pre-existing collider. This would likely be a good place to start.