0
votes

I'm fairly new to Unity, and very new to networking concepts. I'm trying to attach a NetworkManager script containing a custom OnServerAddPlayer function to an empty game object with a NetworkManager game component. However, when I attempt to do this, I receive a "the script class cannot be found... ...ensure the file name and script class match" error.

My research has suggested vaguely that perhaps only MonoBehaviour scripts can be attached to GameObjects (?), but I haven't been able to find anything explicitly stating this anywhere. And if one can't attach NetworkManager scripts to GameObjects, how would one reference variables on the NetworkManager component like startPositions and spawnPrefabs? And does the script need to be attached to a GameObject for public override OnServerAddPlayer (NetworkConnection conn, short playerControllerId) {...} to be called?

Thank you in advance!

Edit: Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

class PlayerManager : NetworkManager {

    int roundRobin = 0;
    public GameObject newPlayerParent;
    NetworkManager manager;

    public override void OnServerAddPlayer (NetworkConnection conn, short playerControllerId) {
        manager = gameObject.GetComponent<NetworkManager>();
        Transform positionObject = manager.startPositions [roundRobin];
        GameObject newPlayer = GameObject.Instantiate(manager.spawnPrefabs[roundRobin], positionObject.position, positionObject.rotation, newPlayerParent.transform);
    }

}

I am trying to attach the script by dragging it from the project view onto the inspector for an empty object with the NetworkManager, NetworkManagerHUD, and NetworkDiscovery components attached.

1
Post your script and explain/show how you are trying to attach it to your object - Programmer

1 Answers

0
votes

Only classes inheriting from Component can be attached to a GameObject, which includes MonoBehaviour. NetworkManager does inherit from MonoBehaviour though, so that's not the problem.

The error message is your best guide here. Make sure your class is in a file called PlayerManager.cs, and there are no other classes defined in the same class.