How to automatic reference another GameObject Transform?
Hi I'm currently trying to make an Enemy AI that follows the Player. The Player can take one enemy and save it, then instantiate later. The problem is when I instantiate a clone of the Enemy AI prefab it will, the transform reference of the player it uses to follow the player is empty.
I tried this in Void Start
[SerializeField]
private Transform _target;
void Start()
{
//Automatic Find the target
_target = GameObject.Find("Player").GetComponent<Transform>();
}
No luck. So my question is how can I Reference my Player's transform in the Enemy AI inspector when they have been instantiated?
Answer by Banditmayonnaise · Feb 19, 2021 at 09:05 PM
Apparently, Void Start doesn't work, I think I need to create some kind of instance to use Void Start. But I just used Void Awake. Void Awake starts when the program is instantiated, so this is perfect
My solution:
private void Awake()
{
//Automatic Find the target
thePlayerObject = GameObject.Find("Player");
_target = thePlayerObject.transform;
}
Your answer
Follow this Question
Related Questions
Im trying to set a spawned enemy a transform on another script 0 Answers
Instantiate Spawns Object Away from Source? 0 Answers
Gameobjects instantiating in the wrong position 0 Answers
2D Unity Instantiate Ragdoll, same Transform as Sprites ( Head, Torso, LArm,RArm,LLeg,RLeg) 2 Answers
Trajectory shooting Tower! 0 Answers