So to explain a little further, I want an enemy from scene 2 to target a player that starts in scene 1. I already have a script for the damage and a AI script for the enemy, to chase the player, but I can not make it target the player, because the player starts in a different scene. I have tried to look for multiple youtube videos as well as asked some of my more savy coding friends.
Also heres the code I have for the enemy chasing the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
public GameObject player;
public float speed;
private float distance;
public string playerTag = "Player";
internal Transform playerObject;
// Start is called before the first frame update
void Start()
{
playerObject = GameObject.FindGameObjectWithTag(playerTag);
}
// Update is called once per frame
void Update()
{
distance = Vector2.Distance(transform.position, player.transform.position);
Vector2 direction = player.transform.position - transform.position;
transform.position = Vector2.MoveTowards(this.transform.position, player.transform.position, speed * Time.deltaTime);
}
So while watching the videos I figured out how to make it target the player within the same scene and I made that work with the same code above. In unity I was able to drag the player into a section below the code. But I felt like I could not find anything about a enemy that attacks a player that comes into the scene later. Maybe I was not looking in the right places, but I spent a lot of time looking.
Edit: I also forgot to say when I save this script I get the erorr: AssetsscriptsEnemyAI.cs(16,24): error CS0029: Cannot implicitly convert type ‘UnityEngine.GameObject’ to ‘UnityEngine.Transform’ and I am not sure how to fix that as well or if it is affecting what I am trying to do.
Edit 2: I have been working on other parts of game and I figured out that I just need to attach the player health to the script but my player starts in another scene and then moves into scene 2.
2
Your enemy can’t find the player because it uses search in start method. As you probably know MonoBehaviour.Start
is called only once in a lifetime after Awake
. documentation
So you have several options:
- You can create some sort of dispatcher class or enemy manager that should have an initialization method that have to be called after scene with player is being loaded, and update all enemies with the instance of
Player
. - You can use Zenject/Extenject or other DI container to inject dependencies to avoid using
GameObject.FindGameObjectWithTag(playerTag);
. You will be able to inject some sort ofAction<Player>
, that Enemy must be subscribed to track Player when it appears. Actually there are dozens of ways to solve the task with DI container. But for your level of question it’s probably would cost a lot of time but still worth it. - You can create
PlayerContainer: ScriptableObject
that has to keep reference to the Player instance. It has to containRegister(Player player)
andPlayer GetInstance()
methods. Then addPlayerContainer
field to the Enemy and Player classes both. The enemy will have access to player throughPlayerContainer.GetInstance()
and the player class has to register itself when it appears. Just DON’T FORGET to check Player instance for NULL when you will callGetInstance
from some method of Enemy class because it could be empty until the player will apear. I suppose that in your case this is the most appropriate way to reach your goal. I also recommend you to watch this video from a conference.
1