Hello! I’ve been working on a small 2D multiplayer game for a little while now. Recently I have been implementing some simple AI to chase the player(s). However, when I try to use GameObject.FindGameObjectWithTag, it throws a NullReferenceException: Object reference not set to an instance of an object
error. I am using Mirror for networking instead of UNet because I had some issues with that. I am able to assign the Target variable by dragging the player object into it ingame, and it works perfectly fine then. I have also tried making other objects with the “AITarget” tag, and that also works! However, as soon as I declare that object as the player prefab in the Mirror NetworkManager, the AI can’t find the object anymore. GameObject.Find doesn’t work either.
AIController script:
using UnityEngine;
using Mirror;
public class AIController : NetworkBehaviour
{
public int speed;
public int turnSpeed;
public Transform target;
Rigidbody2D rb2D;
void Awake()
{
rb2D = GetComponent<Rigidbody2D>();
target = GameObject.FindGameObjectWithTag("AITarget").transform;
}
void FixedUpdate()
{
Move();
Turn();
}
void Move()
{
rb2D.velocity = transform.up * speed * Time.fixedDeltaTime;
}
void Turn()
{
Vector2 direction = (Vector2)target.position - rb2D.position;
direction.Normalize();
float rotateAmount = Vector3.Cross(direction, transform.up).z;
rb2D.angularVelocity = -rotateAmount * turnSpeed * Time.deltaTime;
}
}
PlayerController script:
using UnityEngine;
using Mirror;
public class PlayerController : NetworkBehaviour
{
public int speed;
public int turnSpeed;
public float fireRate;
public GameObject bulletPrefab;
public Transform firePoint;
Rigidbody2D rb2D;
float nextTimeToFire = 0f;
void Awake()
{
rb2D = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if (!isLocalPlayer)
{
return;
}
Move();
Turn();
if (Input.GetMouseButton(0) && Time.time >= nextTimeToFire)
{
CmdFire();
nextTimeToFire = Time.time + 1f / fireRate;
}
}
void Move()
{
rb2D.velocity = transform.up * speed * Time.fixedDeltaTime;
}
void Turn()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)mousePos - rb2D.position;
direction.Normalize();
float rotateAmount = Vector3.Cross(direction, transform.up).z;
rb2D.angularVelocity = -rotateAmount * turnSpeed * Time.deltaTime;
}
[Command]
void CmdFire()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
NetworkServer.Spawn(bullet);
}
public override void OnStartLocalPlayer()
{
GetComponent<SpriteRenderer>().color = Color.blue;
}
}
Full error:
NullReferenceException: Object reference not set to an instance of an object
AIController.Awake () (at <b20ef9cf22714441922445df38f1b92e>:0)
UnityEngine.Object:Instantiate(GameObject, Vector3, Quaternion)
AISpawner:OnStartServer()
Mirror.NetworkIdentity:OnStartServer(Boolean) (at Assets/Mirror/Runtime/NetworkIdentity.cs:373)
Mirror.NetworkServer:SpawnObject(GameObject) (at Assets/Mirror/Runtime/NetworkServer.cs:937)
Mirror.NetworkServer:Spawn(GameObject) (at Assets/Mirror/Runtime/NetworkServer.cs:1035)
Mirror.NetworkServer:SpawnObjects() (at Assets/Mirror/Runtime/NetworkServer.cs:1228)
Mirror.NetworkManager:StartServer() (at Assets/Mirror/Runtime/NetworkManager.cs:261)
Mirror.NetworkManager:StartHost() (at Assets/Mirror/Runtime/NetworkManager.cs:318)
Mirror.NetworkManagerHUD:OnGUI() (at Assets/Mirror/Runtime/NetworkManagerHUD.cs:42)
Sorry if I left anything out and thanks in advance :]