PlayerSetup Gets Errors When Using GameManager’s Variables – MultiPlayer FPS Ep. 7

Updated on November 5, 2017 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on November 5, 2017

Hello! So I am following the multiplayer FPS tutorial, and am on Ep. 7. My problem is that in the PlayerSetup script, when I try to reference Player and _PlayerID, etc, it has the red error line under it saying that “The name “GameManager” does not exist in the current context”. In the tutorial, I didn’t see him do anything to reference the GameManager script in PlayerSetup, but it still works. I don’t know why. Here is my code:

PlayerSetup:

 

 
using UnityEngine;
using UnityEngine.Networking;
 
[RequireComponent(typeof(Player))]
public class PlayerSetup : NetworkBehaviour {
 
[SerializeField]
Behaviour[] componentsToDisable;
 
[SerializeField]
string remoteLayerName = "RemotePlayer";
 
Camera sceneCamera;
 
void Start ()
{
if (!isLocalPlayer)
{
DisableComponents();
AssignRemoteLayer();
} else
{
sceneCamera = Camera.main;
if (sceneCamera != null)
{
sceneCamera.gameObject.SetActive(false);
}
}
}
 
public override void OnStartClient()
{
base.OnStartClient();
 
string _netID = GetComponent().netId.ToString();
Player/*<---Error*/ _player = GetComponent<Player/*<---Error*/>();
 
GameManager/*<---Error*/.RegisterPlayer(_netID, _player);
}
 
void AssignRemoteLayer()
{
gameObject.layer = LayerMask.NameToLayer(remoteLayerName);
}
 
void DisableComponents ()
{
for (int i = 0; i < componentsToDisable.Length; i++)
{
componentsToDisable[i].enabled = false;
}
}
 
void OnDisable ()
{
if (sceneCamera != null)
{
sceneCamera.gameObject.SetActive(true);
}
 
GameManager/*<---Error*/.UnRegisterPlayer(transform.name);
}
}
 

 

and GameManger

 

 
using UnityEngine;
using System.Collections.Generic;
 
public class GameManager : MonoBehaviour {
 
private const string PLAYER_ID_PREFIX = "Player ";
 
private static Dictionary<string, Player> players = new Dictionary<string, Player>();
 
public static void RegisterPlayer (string _netID, Player _player)
 {
 string _playerID = PLAYER_ID_PREFIX + _netID;
 players.Add(_playerID, _player);
 _player.transform.name = _playerID;
 }
 
public static void UnRegisterPlayer (string _playerID)
 {
 players.Remove(_playerID);
 }
 
void OnGUI()
 {
 GUILayout.BeginArea(new Rect(200, 200, 200, 500));
 GUILayout.BeginVertical();
 
foreach (Player _player in players.Keys)
 {
 GUILayout.label(_playerID + " - " + players[_playerID].transform.name);
 }
 
GUILayout.EndVertical();
 GUILayout.EndArea();
 
}
}
 
  • Liked by
Reply