GetPlayerObject always seems to return null

Options
SCY
SCY
edited January 2023 in Fusion

I would like to make an NPC follow a random player.

However, when I try to use this, it always returns null. I can see some use cases of this function for the local player. Does this only work on the local player?

Here is my example code

    List<PlayerRef> list = new List<PlayerRef>();
    foreach (PlayerRef r in Runner.ActivePlayers)
      list.Add(r);
    PlayerTarget = list[Random.Range(0, list.Count)];

    NetworkObject player = Runner.GetPlayerObject(PlayerTarget); //player is always null here


Answers

  • Bremstork
    Options

    Hello !

    Have you set it up before with Runner.SetPlayerObject() ? 😁

  • SCY
    SCY
    edited January 2023
    Options

    I believe it's been set automatically by the Fusion KCC sample beforehand, as I can see all of the players listed underneath Runner.ActivePlayers.

    I am running this within a RPC function though, as I want a navmesh agent to move towards a player. Would this be the correct approach?