2

I am in the process of making a simple tic tac-tac-toe game using the new Unet functionality in Unity to learn how to use the Unet functions.

I have a brain code that spawns the 9 sprites to make up the board and am working on having these set to x's when clicked. I use a [synchvar] to keep track of the state of the sprites and and a hook to change the image when the [SyncVar] changes. The game works perfectly if I am controlling the server but does nothing if I am controlling the client. From what I understand you correct this by adding local player authority to the object and using [command] but this doesn't seem to correct the issue. The code below is what is attached to my sprites. The question has been answered and the code below reflects the correction.

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


public class spacescript : NetworkBehaviour {
    public Sprite x;
    public Sprite o;
    SpriteRenderer sr;
    public int state;
    NetworkIdentity myNetId;

    void Awake ()
    {

        myNetId = GetComponent<NetworkIdentity>();

    }

    public void setstate(int newstate)
    {
        state=newstate;

        if (state == 1) 
        {
            gameObject.GetComponent<SpriteRenderer> ().sprite = x;
        }
    }

    void OnMouseDown()
    {

        GameObject whatplayer = braincode.singleton.GetComponent<braincode> ().returnplayer();
        whatplayer.GetComponent<playerscript> ().thingwasclicked (myNetId, gameObject);


    }




}

my tiles are spawned by the following code

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

public class braincode : NetworkBehaviour {
    public GameObject spaceprefab;
    static public braincode singleton;

    GameObject players;
    public static GameObject brain;
    void Start ()
    {

        singleton = this;
        brain = gameObject;
    }
    public GameObject  returnplayer()
    {
        return players;
    }
    public void add_player(GameObject player)
    {

        players = player;


    }
    public override void OnStartServer()
    {

        for (int x=0; x<3; x++) {
            for (int y=0; y<3; y++) {

                GameObject  space = (GameObject)GameObject.Instantiate(spaceprefab, transform.position, Quaternion.identity);

                space.transform.position = new Vector3 (-3 + (2 * x), -3+ (2*y), 0f);
                NetworkServer.Spawn (space);
            }
        }

    }
}

and my playerobjects have the following code

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

public class playerscript : NetworkBehaviour {


    void Start () 
    {


        if (isLocalPlayer) 
        {

            braincode.brain.GetComponent<braincode> ().add_player (gameObject);

        }
    }

    public void thingwasclicked(NetworkIdentity tiles , GameObject whoclicked)
    {
        Cmdassignstate (tiles,whoclicked);
    }

    [Command]
    public void Cmdassignstate(NetworkIdentity tile, GameObject whoclicked)

    {

        RpcUpdateState (1,whoclicked);

    }

    [ClientRpc]
    void RpcUpdateState(int newState, GameObject tile)
    {
        tile.GetComponent<spacescript> ().setstate (1);

    }




}
2
  • StackOverflow allows answering your own question, if you're so inclined. Nov 11, 2015 at 1:07
  • thanks again for all the help user3071284 Nov 11, 2015 at 5:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.