Setting up player in onServerAddPlayer()
Hi everyone. I'm setting up a game for network play, and am looking to set colors for players as they are added to the game. I've been following this guide Unity - Manual: PlayerObjects
I've followed the guide to the letter as far as I can tell, but am getting compiler errors on my Player script. I've attached the updated version. Player script
I'm also including a screenshot of my game scene. You should be able to see the compiler errors in the console and the inspector tab, which is open to my Player prefab.
Maybe I'm missing something dumb--I'm a Unity noob tbh. Thanks for taking the time.
Answer by jimmyRustler · Nov 02, 2015 at 01:52 AM
This is the fixed version. Used the code in-line tool since file upload isn't working. Anyhow, no more compiler errors.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
class Playerr : NetworkBehaviour
{
[SyncVar]
public Color color;
}
class MyManager : NetworkManager
{
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
GameObject player = (GameObject)Instantiate(playerPrefab, new Vector3(0.0f,0.0f,0.0f), Quaternion.identity);
// Compiler throws error when I try setting Playerr's color property = Color.red
Color red = Color.red;
player.GetComponent<Playerr>().color = red;
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}
Answer by jimmyRustler · Nov 01, 2015 at 03:59 PM
So as mentioned here http://forum.unity3d.com/threads/error-cs0117-unityengine-quaternion-does-not-contain-a-definition-for-indentity.357564/ the example code Unity provided in the link above is wrong. Quaternion does not recognize "Identity' as a property..because it's expecting it in lowercase format. I fixed this and by doing so removed a compiler error. Updated screen attached.
Answer by jimmyRustler · Nov 02, 2015 at 01:52 AM
This is the fixed version. Used the code in-line tool since file upload isn't working. Anyhow, no more compiler errors.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
class Playerr : NetworkBehaviour
{
[SyncVar]
public Color color;
}
class MyManager : NetworkManager
{
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
GameObject player = (GameObject)Instantiate(playerPrefab, new Vector3(0.0f,0.0f,0.0f), Quaternion.identity);
// Compiler throws error when I try setting Playerr's color property = Color.red
Color red = Color.red;
player.GetComponent<Playerr>().color = red;
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}
Your answer
Follow this Question
Related Questions
i want to create a custom network lobby manager. How can i script it? 0 Answers
Pull Playfab users list 0 Answers
Network multiplayer 0 Answers
Networking where each player have multiple characters. 0 Answers
Simple Host - Client Callback 0 Answers