I am networking a 2d game for my school project and I have come across a problem when attempting to make a player "Evolve" in a network scenario. The player will only evolve correctly on the client and not on the host. The code that causes the problem is somewhere in the Evolve script but I don't know how to show the problem through the code, as the issue is in enactment of the code. Hence, I have attached both the code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine;
public class Evolve : NetworkBehaviour {
public bool canEvolve;
public bool isEvolving;
public int evoTimer;
public int timeToEvolve;
public int currentEvo;
public GameObject Evo0;
public GameObject Evo1;
public GameObject Evo2;
public GameObject nextEvo;
public GameObject nextA_Atk;
public GameObject nextB_Atk;
// Use this for initialization
void Start() {
canEvolve = true;
isEvolving = false;
evoTimer = 0;
timeToEvolve = GameObject.FindGameObjectWithTag("Settings").GetComponent<GameSettings>().timeToEvolve;
currentEvo = -1;
RpcCallEvolve();
}
// Update is called once per frame
void Update() {
if (!isLocalPlayer)
{
return;
}
if ((Input.GetButton("Evolve")) && (canEvolve == true))
{
isEvolving = true;
evoTimer += 1;
if (evoTimer >= timeToEvolve)
{
RpcCallEvolve();
}
}
else
{
isEvolving = false;
evoTimer = 0;
}
}
[ClientRpc(channel = 0)]
void RpcCallEvolve()
{
currentEvo += 1;
switch (currentEvo)
{
case 0:
nextEvo = Instantiate(Evo0) as GameObject;
break;
case 1:
nextEvo = Instantiate(Evo1) as GameObject;
break;
case 2:
nextEvo = Instantiate(Evo2) as GameObject;
break;
case 3:
Win(name);
break;
}
GetComponent<SpriteRenderer>().sprite = nextEvo.GetComponent<SpriteRenderer>().sprite;
GetComponent<PolygonCollider2D>().points = nextEvo.GetComponent<PolygonCollider2D>().points;
canEvolve = true;
evoTimer = 0;
Destroy(nextEvo);
}
void Win(string player)
{
Debug.Log("Winner is " + player);
gameObject.SetActive(false);
}
}
And a link to download the full project if that helps people figure out the problem. https://1drv.ms/u/s!ArLjF5CAV1VwgbxZdCQkb_9PZ_j5kw
I don't need the rest of the code fixed or neatened, just info on what might work for the above problem.
Thanks so much to anyone who helps.