@Schneider21
Thanks for your reply. I was working on my code, trying different things and I think that now I got another problem.
I was typing whole long post for 20min and then realised that I’ll just check the editor. It was not about the script, but about attaching it to correct gameObjects… Changed it from “GameController” to “Player” (this one which is spawned with each player)
Now I have other problem, not sure if its getting better or worse. Please don’t mind my naming or the way I am doing it. I am trying to learn and understand, thats why I am going for 3d game. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.Networking;
public class TriggerController : NetworkBehaviour
{
public GameObject xMark;
public GameObject oMark;
Camera cam;
RaycastHit hit;
void Start()
{
cam = Camera.main.GetComponent<Camera>();
}
void Update()
{
if (!isLocalPlayer)
{
return;
}
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Debug.Log(hit.collider.name);
if (Input.GetMouseButtonDown(0))
{
if (hit.transform.CompareTag("trigger"))
{
CmdPlaceMark();
}
}
}
}
[Command]
void CmdPlaceMark()
{
if (!hit.transform.gameObject.GetComponent<TriggerController2>().isMarked) //every trigger (9 at all) has this boolean so when I place some mark, I can't place any mark anymore.
{
if (GameController.currentTurn == GameController.Turn.X)
{
GameObject go = (GameObject)Instantiate(xMark, hit.collider.transform);
NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
GameController.currentTurn = GameController.Turn.O;
}
else {
GameObject go = (GameObject)Instantiate(oMark, hit.collider.transform);
NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
GameController.currentTurn = GameController.Turn.X;
}
hit.transform.gameObject.GetComponent<TriggerController2>().isMarked = true;
}
}
}
And that’s where fun begin. When I play host role in the editor (not doing any builds) it works like it should, spawning objects, debugging win when it should etc. Same when I do build&run and play as a host.
The thing is - when build is host, and editor is client… I can place marks on build(host) and it shows at editor(client), and when I click somewhere at editor, nothing happens(literally, no warning)… and doing the other way around - when editor is host and build is client… when I place mark via editor, it shows at build. When I press somewhere at client(build) I get error pointing at this line:
if (!hit.transform.gameObject.GetComponent<TriggerController2>().isMarked)
Whole line from console:
NullReferenceException: Object reference not set to an instance of an object
TriggerController.CmdPlaceMark () (at Assets/TriggerController.cs:47)
TriggerController.InvokeCmdCmdPlaceMark (UnityEngine.Networking.NetworkBehaviour obj, UnityEngine.Networking.NetworkReader reader)
UnityEngine.Networking.NetworkIdentity.HandleCommand (Int32 cmdHash, UnityEngine.Networking.NetworkReader reader) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:617)
UnityEngine.Networking.NetworkServer.OnCommandMessage (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1281)
UnityEngine.Networking.NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:469)
UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:425)
UnityEngine.Networking.NetworkConnection.TransportReceive (System.Byte[] bytes, Int32 numBytes, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:576)
UnityEngine.Networking.NetworkServer.OnData (UnityEngine.Networking.NetworkConnection conn, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:738)
UnityEngine.Networking.NetworkServer+ServerSimpleWrapper.OnData (UnityEngine.Networking.NetworkConnection conn, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1866)
UnityEngine.Networking.NetworkServerSimple.HandleData (Int32 connectionId, Int32 channelId, Int32 receivedSize, Byte error) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServerSimple.cs:384)
UnityEngine.Networking.NetworkServerSimple.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServerSimple.cs:247)
UnityEngine.Networking.NetworkServer.InternalUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:691)
UnityEngine.Networking.NetworkServer.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:642)
UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:1090)
Anyone? I bet I am just doing something wrong with server-client communication.