Home > Enterprise >  OnStartLocalPlayer() is not overriding
OnStartLocalPlayer() is not overriding

Time:12-25

I make multiplayer script and OnStartLocalPlayer method can't override for some reason. Error:

Assets\Sharoidi\Scripts\PlayerController.cs(10,26): error CS0115: 'PlayerController.OnStartLocalPlayer()': no suitable method found to override

Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using UnityEngine.Audio;
using TMPro;

public class PlayerController : NetworkBehaviour
{
    public override void OnStartLocalPlayer() {
        Debug.Log("Local Player started!");
    }
}

I tried to delete override, but now method doesn't get called when local player spawns.

CodePudding user response:

My guess would be that you're duplicating names of built-in classes and then not getting the results you want because Unity doesn't know which class to use. In your snippet you're defining a class called PlayerController, but that's already the name of a different class and that class doesn't have the OnStartLocalPlayer method.

This shouldn't be giving you this particular error, though, but it makes me wonder about your NetworkBehaviour class. That is also a built-in class, but it's in the UnityEngine.Networking namespace, which you are not using in your snippet. Again, this should give you a different error, that "it's not defined, are you missing an assembly or using statement," but you're not getting that error, which makes me think maybe you have a script somewhere in your project called NetworkBehaviour that's shadowing the built-in.

Try replacing your class declaration with this and see if this does anything different:

public class PlayerController : UnityEngine.Networking.NetworkBehaviour
{

This would point explicitly to the built-in NetworkBehaviour. If this fixes it for you then great, but really stop giving your stuff the same names as built-in things.

CodePudding user response:

You are attempting to override a callback function. You need to instead register a function for the callback.

See: Network Behaviour - OnStartLocalPlayer

and

What is a CallBack?

  • Related