in Unity 2018.2.20f1: I am making a top down shooter(2d) and have a enemy class for all enemy AI to derive from. Pretty simple. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class AI : MonoBehaviour { public int health; public float speed; public float timeTweenAttack; [HideInInspector] public Transform player; public int damage; // Use this for initialization void Start () { player = GameObject.FindGameObjectWithTag("Player").transform; } // Update is called once per frame void Update () { } public void takedamage(int damageAmount) { health -= damageAmount; if (health <= 0) { Destroy(this.gameObject); } } } And yet, player = GameObject.FindGameObjectWithTag("Player").transform; throws up a Object not set to an instance of an object. Error. The player is a prefab, if that matters. What do I do?
You don't have a GameObject tagged with "Player" active in the scene at the time this Start method is called. If you are later instantiating this object, wait until it exists before you try to grab a reference to it. Or better yet, just cache the reference Instantiate returns in the first place.