Vladutzu27
Vladutzu27

Reputation: 57

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

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.

Upvotes: 0

Views: 40

Answers (1)

Paweł Łęgowski
Paweł Łęgowski

Reputation: 830

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.

Upvotes: 1

Related Questions