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 /
avatar image
0
Question by roiek · Dec 26, 2014 at 10:53 PM · c#networkingmultiplayer

[PhotonUnityNetwork] Synchronize colors among players.

Hi! Im trying to make a simple scene, where i can spawn up to 6 players, each with different color, but colors cannot repeat themeselves and look the same on different clients.

(RPV -> RoomPhotonView) The idea was to:

In UI:

 void OnJoinedRoom(){
     GameLogics.SetActive(true);

     int id1 = PhotonNetwork.AllocateViewID();
     PhotonView photonView = this.GetComponent<PhotonView>();

     player = new PhotonPlayer(true, Random.Range(0, 1000), "Player" + Random.Range(0, 1000));
     if (!PhotonNetwork.isMasterClient)
        RPV.RPC("RequestColor", PhotonTargets.MasterClient, player);
     RPV.RPC("SpawnOnNetwork", PhotonTargets.AllBuffered, transform.position, transform.rotation, id1, player);
     Menu.SetActive(false);
 }


On ServerSide:

 [RPC]
 void RequestColor(PhotonPlayer pp)
 {
     Debug.Log("hehe");
     RPV.RPC("SetColor",pp,isColorTaken);
     for (int i = 0; i < isColorTaken.Length; i++)
         Debug.Log(isColorTaken[i]);
 }
 [RPC]

 void SetColor(bool[] arr)
 {
     isColorTaken = arr;
 }

[RPC]

 void SpawnOnNetwork(Vector3 pos, Quaternion rot, int id1, PhotonPlayer np)
 {
     Transform newPlayer = Instantiate(playerPrefab, pos, rot) as Transform;
     PhotonView[] nViews = newPlayer.GetComponentsInChildren<PhotonView>();
     nViews[0].viewID = id1;
     newPlayer.GetComponentInChildren<MeshRenderer>().material.color = randomNotTakenColor();

}

 Color randomNotTakenColor(){
     int r = Random.Range(0, 5);
     if (!isColorTaken[r])
     {
         isColorTaken[r] = true;
         return colorArray[r];
     }
     else
         return randomNotTakenColor();
 }



I know that in this sample we get problems when someone connects then disconnects due to not 'falseing' isColorTaken of his color, but for now I just want to make it work in any way.

I'm sure the solution is really easy, but im afraid i got some kind of mindblock.

Thank you.

Comment
Add comment · Show 1
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 roiek · Dec 26, 2014 at 01:51 PM 0
Share

I made it to synchronize colors on each client, but still i got some problems with making each color unique. Shall i pass something like 'RandomUnusedColor' via RPC from masterClient? is this idea any good?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Navigatron · Dec 26, 2014 at 11:27 PM

After thinking about this for a couple of minutes, I have had an idea. Photon instantiates all other players on your client as soon as you join a room, And I believe it does it in order. Therefore, we don't need any network calls to solve this.

On any script in the game, throw in this code:

     private static int colorIndex = -1;
     public static int ColorIndex{
         get{
             colorIndex+=1;
             if(colorIndex>=colorsDefined){colorIndex=0;}//where colorsDefined is the number of defined Colors
             return experience;
         }
     }

That provides an int that can be accessed by anything in the scene, and increments itself whenever retrieved.

Finally, On your player Object:

      public Color[] colors; //Public so colors can be assigned in the Inspector.
      void Awake(){//As soon as the GameObject is Instantiated
           renderer.material.color = colors[OtherClass.ColorIndex];
      }

Where OtherClass is whatever other class you threw the other code into. If you wait a tad before instantiating the local player, (Put Photon.Instantiate into the Start rather than Awake functions), then All other players should instantiate on your client and grab their colors in the correct order. Woohoo!

Unfortunately, this also does not help when a Player leaves the room. In order to include that feature, I would assign each players color index to their custom properties. Then you can find their color index when they leave, and use a more complicated system of boolean arrays to determine if an index is available.

On Photon Player Disconnected:

http://doc-api.exitgames.com/en/pun/current/pun/doc/group__public_api.html#ggaf30bbea51cc8c4b1ddc239d1c5c1468fa39b1e276b8dab1e7620a643b7e01d71c

A final thought: If the players cannot change their color in-game, then do they really need to be synchronized? If not, then the above code should work perfectly.

Best of Luck, ~Navi

Comment
Add comment · Show 2 · Share
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 HakJak · Dec 04, 2018 at 08:44 PM 0
Share

I know this post is super old, but thank you! This was very helpful.

avatar image Giantbean · Dec 22, 2020 at 08:31 PM 0
Share

Old post but I I don't get the "return experience;" What variable do I want to use instead of experience if I m just changing the multi player character color.

Also as the first bit can be "On any script in the game" could it go on the second script?

Updated with new render call as the old method is depreciated.

 using UnityEngine;
 
 public class ColorIndexPicker : $$anonymous$$onoBehaviour
 {
     Renderer ObjectRender;
     public Color[] colors; //Public so colors can be assigned in the Inspector.
     void Awake()
     {//As soon as the GameObject is Instantiated
         ObjectRender = GetComponent<Renderer>();
         ObjectRender.material.color = colors[ColorIndex];
     }
 
     private static int colorIndex = -1;
     public static int ColorIndex
     {
         get
         {
             colorIndex += 1;
             if (colorIndex >= 6) { colorIndex = 0; }//where colorsDefined is the number of defined Colors
             return What?;
         }
     }
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Client can't spawn GameObject's 0 Answers

[Command] Attribute Returning Warnings and Errors 0 Answers

Unity3d Csharp Networking Error 3 Answers

How To Deal With Lingering Prefabs in Multiplayer Scene ? 0 Answers


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