Home > database >  How do I call another GameObject's possition in a Prefab Instantiated Gameobject's script?
How do I call another GameObject's possition in a Prefab Instantiated Gameobject's script?

Time:07-05

If that is not possible, can I at least find it using raytracing?

I want to make some enemy GameObjects (which are instantiated from a prefab, which spawn randomly) always look towards the player.

I don't think I can just call it's xyz coordinates, but maybe I can find them using raytracing? How exactly do I do that? Even if I know what raytracing is and used it before in other non-unity projects, I have no idea how to implement it in unity.

CodePudding user response:

Your enemies should be able to get reference to player somehow (probably Singleton for entry level, or Dependency Injection for pros) and use player.transform.position

So basically your player should look like that:

public class Player : MonoBehaviour 
{
    public static Player Instance;

    void Awake()
    {
        Instance = this;
    }
}

And then enemy can use:

Player.Instance.transform.position

to get players position.

  • Related