I'm new to Unity. I also new to Stackoverflow. Nice to meet you all!
I am using Unity version 2021.3 LTS
Here the details of my problem :
What did I try:
What were I expecting :
*How can we make the animal keep following player that a clone?*
I'm using debug.log for targetPlayer.name. It identified my selected character's name that spawned at scene Game.
Here my Code NPC_Follow.cs , I attached it on the animal
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NPC_Follow : MonoBehaviour
{
private GameObject targetPlayer;
NavMeshAgent myAgent;
// Start is called before the first frame update
void Start()
{
targetPlayer = GameObject.FindGameObjectWithTag("Player");
Debug.Log("Name: " + targetPlayer.name);
myAgent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
myAgent.destination = targetPlayer.transform.position;
}
}
I also put LoadCharacter.cs if needed to see. LoadCharacter.cs, attached to empty gameobject at game scene. for spawning the selected character
public class LoadCharacter : MonoBehaviour
{
public GameObject[] characterPrefabs;
public Transform playerStartPosition;
private string selectedCharacterDataName = "SelectedCharacter";
int selectedCharacter;
public GameObject playerObject;
// Start is called before the first frame update
void Start()
{
selectedCharacter = PlayerPrefs.GetInt(selectedCharacterDataName, 0);
playerObject = Instantiate(characterPrefabs[selectedCharacter],
playerStartPosition.position,
characterPrefabs[selectedCharacter].transform.rotation);
}
}
Thank you and have a nice day!
To answer my question, I found solution
Debugging :
What I did thinking :
What I did:
Transform childTransform = playerCharacter.transform.Find("Player Name");
I will test later if I could try change tag EmptyGameObject Parent to none or any. so it could try searching where child gameobject that have tag "player"