Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Victov · May 24, 2012 at 07:29 PM · instantiatevariablesrpcresetteams

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

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · May 24, 2012 at 07:55 PM 0
Share

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.

avatar image Victov · May 24, 2012 at 08:05 PM 0
Share

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.

avatar image whydoidoit · May 24, 2012 at 08:08 PM 0
Share

Is this script on a Scene object rather than an instantiated one? It's probably ok if it is a scene one

avatar image Victov · May 24, 2012 at 08:14 PM 0
Share

It's part of my player-prefab that's instantiated when joining/creating the server

avatar image Victov · May 25, 2012 at 03:55 PM 0
Share

Never$$anonymous$$d fixed it myself, thanks for your time!

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to reset a variable? 1 Answer

Reset script variable values automatically? 2 Answers

Mouse Over, Mouse Down, and Instantiate. 1 Answer

Null Reference Exception on instantiated objects script. 1 Answer

Photon Network Instantiate Objects over Network 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges