So, I keep getting this error "NullReferenceException: Object reference not set to an instance of an object SJ_sensor.OnTriggerEnter (UnityEngine.Collider target)" whenever I enter the trigger collider and Void Attack doesn’t do what it’s supposed to do.
Here is the reference code:
SJ_Sensor
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SJ_sensor : MonoBehaviour
{
public GameObject myObject; //make ref. in inspector window
void OnTriggerEnter(Collider target)
{
if(target.tag == "Player")
{
this.GetComponent<Sniper_Joe>().Attack();
Debug.Log("I See You");
}
}
void OnTriggerExit(Collider other)
{
Debug.Log("Ok, Now I don't see you");
}
}
Sniper_Joe
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sniper_Joe : MonoBehaviour
{
public GameObject Player;
public GameObject You;
public Animator animator;
public float dist;
public float vision = 15f;
public Transform target;
public bool Isee;
void Start()
{
animator.SetBool("InRange", false);
}
void Update()
{
if (Isee == true)
{
transform.rotation = Quaternion.LookRotation(transform.position - target.position);
}
}
public void Attack()
{
animator.SetBool("InRange", true);
Isee = true;
}
public void Block()
{
animator.SetBool("InRange", false);
Isee = false;
}
}
Well, does the GameObject with the tag “Player” have the Sniper_Joe script attached? Is there another GameObject with a collider and the Player tag? Maybe a child of the player?
Also, don’t use tag == "Something", but rather CompareTag("Something"). It’s a lot more efficient, you can probably find something in the docs about it.
Why will I need to attach the Sniper Joe script to the player? The enemy has both the Sniper Joe Script and the SG_Sensor in order to track player movement and to check if it’s in his “line of sight”.
“Player” is the tag I gave to a GameObject storing the models, movement and scripting of the player.
3.I tried using CompareTag("Something") but I got the Error:
UnassignedReferenceException: The variable myObject of SJ_sensor has not been assigned.
You probably need to assign the myObject variable of the SJ_sensor script in the inspector.
SJ_sensor.OnTriggerEnter (UnityEngine.Collider target)
You can of course continue trying random combinations, but if you’re interested in returning to productive work soon, I urge you to start the only 3-step process that will actually fix this error, short of random chance being lucky for you.
Sorry I thought it said target.GetComponent<Sniper_Joe>(). In that case I don’t really see a problem here. But since you’re getting that error with CompareTag that literally tells you to assign something in the Inspector, now I’m thinking you forgot to assign something in the Inspector an are getting a NullReferenceException because of that.
I mean, you didn’t even say which line the error is coming from. I’m just guessing here, as the problem might not even be in what you’ve posted.
Honestly, there is something wrong with my Unity, or maybe it’s Visual Code. Everytime I click the debug log error, it directs me to the script that needs correcting, but not the line.