- Home /
Variable not being saved?
Hello everybody, I've encountered something strange today while making my auto-assign function for the teams in my game. The variables I have to check the amounts of players don't seem to be saved. In my player class I've got the following functions (with a lot of other stuff left out of it, since it's not important)
 using UnityEngine;
 using System.Collections;
 using System;
 
 public class PlayerControl : MonoBehaviour
 {
     //server vars
     private int RedTeamCount = 0;
     private int BlueTeamCount = 0;
     //client vars
     public int playerTeam = 0; //0noteam 1red 2blue
     
     void Start()
     {
         if(Network.isServer)
         {
             playerTeam = 1;//just start filling at red
             RedTeamCount++;
         }
         
         //should we be enabled?
         if(!networkView.isMine)
         {
             enabled = false;
         }
         else
         {            
 
         networkView.RPC("CompleteInitialize", RPCMode.Server, networkView.viewID);
 
         }
     }
     void OnGUI()
     {
         GUI.Box(new Rect(100,10,100,20), "team: " + this.playerTeam);//used for debugging
 
     
     }
     
     [RPC]
     void ReceiveTeamAssign(NetworkViewID Who, int TeamNr)
     {
         if(Who.isMine) //if I'm the one who gets his team assigned
             playerTeam = TeamNr;
         
         if(CombatHandler.GetPlayerByID(Who) != null) //check
         {
             GameObject P = CombatHandler.GetPlayerByID(Who);
             if(TeamNr != playerTeam) //if on the other side
                 P.gameObject.GetComponentInChildren<TextMesh>().renderer.material.color = Color.red; //if it's an enemy make his nameplate red
         }
     }
 
     //server functions!
     [RPC]
     void CompleteInitialize(NetworkViewID Who)
     {
         if(!Network.isServer)
             return;
         
         Debug.Log("Someone joined, red team: " + RedTeamCount + ", blue team: " + BlueTeamCount);
         if(RedTeamCount > BlueTeamCount)
         {
             BlueTeamCount++;
             networkView.RPC("ReceiveTeamAssign", RPCMode.AllBuffered, Who, 2);
         }
         else
         {
             RedTeamCount++;
             networkView.RPC("ReceiveTeamAssign", RPCMode.AllBuffered, Who, 1);
         }
     }
 }
this should basically assign the teams properly, but here comes the problem, the variables RedTeamCount and BlueTeamCount always remain 0. This class gets instantiated every time a player joins (but I don't really think that's the problem) the "receiveteamassign" function DOES arrive, so theoretically BlueTeamCount++/RedTeamCount++ should be called too!
I have no idea what to do, since this should work theoretically. All players get assigned to Red (since red !> blue (they're both 0)) while red should always be 1, since the server itself is in team red!
Thanks in advance, if you need more information feel free to ask!
-Victov
And you have a server initialized when this code starts right? I had a problem where networkviews in Start aren't ready yet when you initialize from a prefab, they are by the first Update call so I do $$anonymous$$e in there with a flag.
I can try that, to put it in Update for the first time, the function gets called though, the networkview.RPC gets called and does arive at AllBuffered... I've put a Debug.Log in that CompleteInitialize function and it does log the results.
Is this script on a Scene object rather than an instantiated one? It's probably ok if it is a scene one
It's part of my player-prefab that's instantiated when joining/creating the server
Never$$anonymous$$d fixed it myself, thanks for your time!
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                