Authorative Movement. NullReferenceException: Object reference not set to an instance of an object

edited March 16 in BOLT Engine
Hello forum!

I am currently implementing Authorative Movement with the help of the "Advanced Tutorials" in the docs. I have a problem right now. Error code below. The host gets an error when instantiating a client's player... I think? Anyways the code is below. If anything else is needed, I am happy to provide it.

I can't find any differences between my code and the code from the tutorial (except for some naming). Any ideas on what the problem might be?

Cheers,
Laurence :)

Error: NullReferenceException: Object reference not set to an instance of an object (In line 25)
[BoltGlobalBehaviour(BoltNetworkModes.Server, "Car")]
public class LB_Multiplayer_ServerCallbacks : Bolt.GlobalEventListener
{
        void Awake()
        {
            LB_PlayerObjectRegistry.CreateServerPlayer();
        }

        public override void Connected(BoltConnection connection)
        {
            LB_PlayerObjectRegistry.CreateClientPlayer(connection);
        }

        public override void SceneLoadLocalDone(string map)
        {
            LB_PlayerObjectRegistry.ServerPlayer.Spawn();
        }

        public override void SceneLoadRemoteDone(BoltConnection connection)
        {
            LB_PlayerObjectRegistry.GetPlayer(connection).Spawn();
        }
}

public static class LB_PlayerObjectRegistry
    {
        static List<LB_PlayerObject> players = new List<LB_PlayerObject>();

        static LB_PlayerObject CreatePlayer(BoltConnection connection)
        {
            LB_PlayerObject player;

            player = new LB_PlayerObject();
            player.connection = connection;

            if (player.connection != null)
            {
                player.connection.UserData = player;
            }

            // add to list of all players
            players.Add(player);

            return player;
        }

        public static IEnumerable<LB_PlayerObject> AllPlayers => players;

        public static LB_PlayerObject ServerPlayer
        {
            get { return players.Find(player => player.IsServer); }
        }
    
        public static LB_PlayerObject CreateServerPlayer()
        {
            return CreatePlayer(null);
        }

        public static LB_PlayerObject CreateClientPlayer(BoltConnection connection)
        {
            return CreatePlayer(connection);
        }

        public static LB_PlayerObject GetPlayer(BoltConnection connection)
        {
            if (connection == null)
            {
                return ServerPlayer;
            }

            return (LB_PlayerObject) connection.UserData;
        }
    }
public class LB_PlayerObject
    {
        public BoltEntity character;
        public BoltConnection connection;

        public bool IsServer => connection == null;

        public bool IsClient => connection != null;
        
        public void Spawn()
        {
            if (!character)
            {
                character = BoltNetwork.Instantiate(BoltPrefabs.Car, RandomPosition(), Quaternion.identity);
                BoltNetwork.Instantiate(BoltPrefabs.Canvas);

                if (IsServer)
                {
                    character.TakeControl();
                }
                else
                {
                    character.AssignControl(connection);
                }
            }

            // teleport entity to a random spawn position
            character.transform.position = RandomPosition();
        }

        Vector3 RandomPosition()
        {
            return new Vector3(5+UnityEngine.Random.Range(0,50), 5.0f, 10);
        }
    }

Answers

  • @stanchion could you help me please :smile: ? I bet it is something simple but I dont get it. I was just converting my code to the authorative movement..
Sign In or Register to comment.