How can I make my enemy target a player who enters the scene?

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:

  1. 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.
  2. 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 of Action<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.
  3. You can create PlayerContainer: ScriptableObject that has to keep reference to the Player instance. It has to contain Register(Player player) and Player GetInstance() methods. Then add PlayerContainer field to the Enemy and Player classes both. The enemy will have access to player through PlayerContainer.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 call GetInstance 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật