• 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
1
Question by zak666 · Oct 12, 2017 at 05:28 AM · playerscoreordering

Organising player Score from highest to lowest?

Hi gang! I managed to sync my players scores in Multiplayer witch is Awesome! I want to add a feature that determines the winner.
(Orders highest Score and Lowest scores) (float) I have each players scores streamed all I am missing is a way top put them order So the results can be displayed.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using Photon;
 public class ScoreTRacker : Photon.MonoBehaviour {
 
     public float Player1Score;
     public float Player2Score;
     public float Player3Score;
     public float Player4Score;
 
     public float AcualPlayer1Score;
     public float AcualPlayer2Score;
     public float AcualPlayer3Score;
     public float AcualPlayer4Score;
 
 
     public Text FistPlace;
     public Text SecondPlace;
     public Text FourthPlace;
     public Text FithPlace;
 
     public float FistPlaceScore;
     public float SecondPlaceScore;
     public float FourthPlaceScore;
     public float FithPlaceScore;
 
     public GameConnectClassic  GameConnect;
     public PlayerGUI PlayerG;
     public GameObject GameMananger;
 
     // Use this for initialization
     void Start () {
         GameMananger = GameObject.FindGameObjectWithTag ("GameMananger");
         GameConnect = GameMananger.GetComponent<GameConnectClassic> ();
     }
     
     // Update is called once per frame
     void Update () {
         if(GameConnect.Player1 = true){
             Player2Score = AcualPlayer2Score;
             Player3Score = AcualPlayer3Score;
             Player4Score = AcualPlayer4Score;
         }
 
         if(GameConnect.Player2 = true){
             Player1Score = AcualPlayer1Score;
             Player3Score = AcualPlayer3Score;
             Player4Score = AcualPlayer4Score;
         }
 
         if(GameConnect.Player3 = true){
             Player2Score = AcualPlayer2Score;
             Player1Score = AcualPlayer1Score;
             Player4Score = AcualPlayer4Score;
         }
 
         if(GameConnect.Player4 = true){
             Player2Score = AcualPlayer2Score;
             Player3Score = AcualPlayer3Score;
             Player1Score = AcualPlayer1Score;
         }
 
         if (PlayerG.GameGoing == false) {
             DetermainWinner ();
         }
     }
 
 
     void  DetermainWinner(){
         // get the highest score.....
         FistPlace.text = FistPlaceScore;
         SecondPlace.text = SecondPlaceScore;
         FourthPlace.text = FourthPlaceScore;
         FithPlace.text = FithPlaceScore;
     }
 
 
 
 
 
 
 
 
 
 
             // SYNC SCORES.......
             
     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
     {
         //we are reicieving data
         if (stream.isReading)
         {
             //receive the next data from the stream and set it to the truLoc varible
             if(GameConnect.Player1 = true){
                 AcualPlayer2Score = (float)stream.ReceiveNext(); //the stream send data types of "object" we must typecast the data into a Vector3 format
                 AcualPlayer3Score = (float)stream.ReceiveNext();
                 AcualPlayer4Score = (float)stream.ReceiveNext();
 
             }
 
 
             if(GameConnect.Player2 = true){
                 AcualPlayer1Score = (float)stream.ReceiveNext(); //the stream send data types of "object" we must typecast the data into a Vector3 format
                 AcualPlayer3Score = (float)stream.ReceiveNext();
                 AcualPlayer4Score = (float)stream.ReceiveNext();
 
             }
 
             if(GameConnect.Player3 = true){
                 AcualPlayer2Score = (float)stream.ReceiveNext(); //the stream send data types of "object" we must typecast the data into a Vector3 format
                 AcualPlayer1Score = (float)stream.ReceiveNext();
                 AcualPlayer4Score = (float)stream.ReceiveNext();
 
             }
 
             if(GameConnect.Player4 = true){
                 AcualPlayer2Score = (float)stream.ReceiveNext(); //the stream send data types of "object" we must typecast the data into a Vector3 format
                 AcualPlayer3Score = (float)stream.ReceiveNext();
                 AcualPlayer1Score = (float)stream.ReceiveNext();
 
             }
 
 
         }
         //we need to send our data
         else
         {
             //send our posistion in the data stream
             if(GameConnect.Player1 = true){
                 stream.SendNext(Player1Score);
             }
 
             if(GameConnect.Player2 = true){
                 stream.SendNext(Player2Score);
 
             }
 
             if(GameConnect.Player3 = true){
                 stream.SendNext(Player3Score);
 
             }
 
             if(GameConnect.Player4 = true){
                 stream.SendNext(Player4Score);
 
             }
         }
     }
 
 }
 
 
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBART82 · Oct 12, 2017 at 07:01 AM

Hello there! @zak666 I have come up with a great way to sort through the scores to get a list of scores from the highest number to the smallest number. Here it is:

     // Create a list to hold all four scores
     public List<int> scores;
 
     void DetermineWinner()
     {
         // Add each players score to the 'score' list.
         scores.Add(Player1Score);
         scores.Add(Player2Score);
         scores.Add(Player3Score);
         scores.Add(Player4Score);
 
         // Use the sorting function to sort out numbers
         // from smallest to largest
         scores.Sort();
 
         // Create a temp list called 'finalList'
         List<int> finalScores = new List<int>();
         // Iterate through the scores backwards to get
         // scores from greates to smallest number.
         for (int i = scores.Count; i --> 0;)
         {
             finalScores.Add(scores[i]);
         }
 
         
         // Set the main scores to the greatest to smallest score.
         scores = finalScores;
     }

If there is anything which you need me to clarify, be sure to let me know!

TBART82

Comment
Add comment · Show 5 · 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 Der-Die-Das · Oct 12, 2017 at 08:14 AM 0
Share

You can leave the for loop and the finalScores list out by using the List.Reverse() Function. With your solution you will not know what player had what score. I know that OP didn't mention that behaviour but I think he will need it.

avatar image TBART82 Der-Die-Das · Oct 12, 2017 at 11:17 AM 0
Share

Thanks for the corrections. I will update my answer soon.

avatar image zak666 · Oct 13, 2017 at 05:26 AM 0
Share

Thanks a lot $$anonymous$$8, Yeah I figured how to Display the Scores In order of player once I had the scores In a order Why didn't i think of lists before.... XD

avatar image zak666 · Oct 13, 2017 at 05:44 AM 0
Share
 void  DetermainWinner() {
 
         // Add each players score to the 'score' list.
         scores.Add(Player1Score);
         scores.Add(Player2Score);
         scores.Add(Player3Score);
         scores.Add(Player4Score);
 
         // Use the sorting function to sort out numbers
         // from smallest to largest
         scores.Sort();
 
         FistPlaceScore = scores.IndexOf(1);
         SecondPlaceScore = scores.IndexOf(2);
         ThirdPlaceScore = scores.IndexOf(3);
         FourthPlaceScore = scores.IndexOf(4);
 
         // get the highest score.....
         FistPlace.text = FistPlaceScore.ToString();
         SecondPlace.text = SecondPlaceScore.ToString();
         ThirdPlace.text = ThirdPlaceScore.ToString();
         FourthPlace.text = FourthPlaceScore.ToString();
 
 
         if(PlayerG.LocalScore == FistPlaceScore){  // I A$$anonymous$$ THE WINNER........
             RewardsFirstPlaceGO.SetActive(true);
         }
 
         if(PlayerG.LocalScore == SecondPlaceScore){  // I A$$anonymous$$ THE Second........
             RewardsFirstPlaceGO.SetActive(true);
         }
 
         if(PlayerG.LocalScore == ThirdPlaceScore){  // I A$$anonymous$$ THE Third........
             RewardsFirstPlaceGO.SetActive(true);
         }
 
         if(PlayerG.LocalScore == FourthPlaceScore) {  // I A$$anonymous$$ THE forth........
             RewardsFirstPlaceGO.SetActive(true);
         }
 
 
 
     }
 
 
 
 
avatar image Der-Die-Das zak666 · Oct 13, 2017 at 05:47 AM 0
Share

scores.IndexOf(1) will return you the index of the element with the value 1. Don't know if you really want that. you can just access the highest value in the list by:

  scores[0]

And add a scores.Reverse() after scores.Sort() to get the right order

avatar image
0

Answer by Der-Die-Das · Oct 12, 2017 at 06:29 AM

So you just want to know which Player won? And you have all the scores ready?

Just compare the Scores? Either with if statements or with Mathf.Max()

And to say something about your code.. It's very redundant. Create a Player class and add the scores etc in it. Afterwards you can loop through a array of the players and you would have way less code than that. Write me again if you would like to get more information about that. ^^

Also.. What Photon Tutorial did you use? I really would like to learn networking :3

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 zak666 · Oct 13, 2017 at 05:20 AM 0
Share

Spent ages figuring out how to use it on the documents. :) After I publish my game ($5 per month to play) I'm going to make a tutorial series.

avatar image Der-Die-Das zak666 · Oct 13, 2017 at 05:50 AM 0
Share

Do you have a YouTube Channel where you will publish it? And how will it take you approx to make the tutorial? Would love to learn from you :pp

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

81 People are following this question.

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

Related Questions

Getting playerID from the Google Play Leaderboard? 0 Answers

Score Question Java Script, problem 1 Answer

Does any know how to hide and show 2d sprites in unity 5 by using a 2d boxcollider set to trigger when player collides with the boxcollider 1 Answer

high score script? 0 Answers

live system for sword 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