- Home /
Recast hit get component: NullReferenceException: Object reference not set to an instance of an object
Ok everything is set up on both objects and assigned in the inspector no idea why Im getting this: NullReferenceException: Object reference not set to an instance of an object
Player: // SeconderyWeaponLockon____
if (Input.GetButtonUp ("LockOn")) { // TOGGLES LOCK ON TO BE ACTIVE OR NOT ACTIVE
if (LockedOnActive == false) {
LockedOnActive = true;
} else {
LockedOnActive = false;
Crosshair1.SetActive (true);
Crosshair2.SetActive (false);
}
}
if (LockedOnActive == true) { // if lock on is active change crosshairs and activate raycast.
Vector3 fwd = transform.TransformDirection(Vector3.forward); //the direction the player is facing
Crosshair1.SetActive (false);
Crosshair2.SetActive (true);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (transform.position, fwd, 60000))
if (Hit.collider.tag == "Red_Team") {
Hit.transform.gameObject.GetComponent<LockedOn>().IsLockedon = true;
LockedOnTarget = Hit.collider.gameObject;
}
}
//_____________________________________________________________________________________________________________
Object Hit Script:
using UnityEngine;
using System.Collections;
public class LockedOn : MonoBehaviour {
// put this on all Enemy Ships and Targets that can be targeted
public bool IsLockedon;
public GameObject LockedOnSymbol;
// Update is called once per frame
void Update () {
if (IsLockedon == true) {
LockedOnSymbol.SetActive (true);
}
}
}
Answer by AurimasBlazulionis · Sep 13, 2016 at 08:02 PM
First of all. You wrote Hit 2 times. Did you mean hit?
Second. Raycast could have hit a child object so you would need to change Hit.collider.tag with hit.transform.root.tag, Hit.transform.gameObject... with hit.transform.root.gameObject... and Hit.collider.gameObject with hit.transform.root.gameObject to make sure you check everything with the root gameObject in hierarchy.
The solution.
You did not set hit to anything. How can unity know to which value put all that information? Well, you did not tell so you should fix it. Put out hit as the argument before maximum distance. Now you should be good to go!
So the line looks like if (Physics.Raycast (transform.position, fwd, out hit, 60000))
Everything in the hirachy is correct and tagged correctly changed to:
if (LockedOnActive == true) { // if lock on is active change crosshairs and activate raycast.
Vector3 fwd = transform.TransformDirection(Vector3.forward); //the direction the player is facing
Crosshair1.SetActive (false);
Crosshair2.SetActive (true);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (transform.position, fwd, 60000))
if (hit.transform.root.tag == "Red_Team") {
hit.transform.root.gameObject.GetComponent<LockedOn>().IsLockedon = true;
LockedOnTarget = hit.transform.root.gameObject;
}
}
tho i am still getting null reference message Im storing the hit.GameObject so when the player fires a missile i can send that information to the missile so it locks on.
Could you write the line which gets the null reference exception? So I could get the idea what's wrong
if (hit.transform.root.tag == "Red_Team") {
tho my game object has the correct tag or Red_Team and has a box collider and rigid body so i don't know why its not picking it up.
Your answer
Follow this Question
Related Questions
GUI showUP when Raycasting 0 Answers
in rewarded ad which code means user closed the ad by itself 2 Answers
RayCast to test if player is grounded 1 Answer
Turn in the direction of movement 0 Answers
need help on raycast 1 Answer