So I’m trying to make the character look to the closest enemy. And my code for it is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemyDetect : MonoBehaviour
{
private void OnTriggerStay(Collider targett)
{
if (targett.tag == "Enemy")
{
Player playerScript = GameObject.Find("Character").GetComponent<Player>();
playerScript.target = targett.gameObject;
}
}
}
So basically what I’ve done is I put in a sphere collider as a child of my character and put this code inside the collider but everytime I run the code it gives an error saying: “UnassignedReferenceException: The variable target of Player has not been assigned.
You probably need to assign the target variable of the Player script in the inspector.”
Can someone help?
Very likely that the error is not coming from this script. Also don't GameObject.Find the character every frame. Your enemyDetect is probably part of the character. Just store the reference in a field somewhere
– Pangamini