Skip to content

Instantly share code, notes, and snippets.

@James-Frowen
Last active October 29, 2020 03:23
Show Gist options
  • Save James-Frowen/4c784ad4122c0219c59ca4d4abf5a778 to your computer and use it in GitHub Desktop.
Save James-Frowen/4c784ad4122c0219c59ca4d4abf5a778 to your computer and use it in GitHub Desktop.
Reference to NetworkBehaviour that is not currently spawned on client (useful for synclists)
// player script (in its own file)
public class Player : NetworkBehaviour { }
// reference to Player that can be sent over network
public struct PlayerReference
{
public uint netid;
private Player _player;
public PlayerReference(Player value) : this()
{
_player = value;
if (value != null)
netid = value.netId;
}
public Player Player
{
get
{
if (_player == null)
{
NetworkIdentity identity = NetworkIdentity.spawned[netid];
if (identity != null)
{
_player = identity.GetComponent<Player>();
}
}
return _player;
}
}
public static implicit operator Player(PlayerReference reference) => reference.Player;
public static implicit operator PlayerReference(Player value) => new PlayerReference(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment