• 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 Atzig · Jun 21, 2015 at 06:06 PM · comparesort

Retrieve float from many different objects?

So I have this script attached to 8 different objects in a scene, one is the player.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
   
 
 public class HealthScoring : MonoBehaviour {
 
     
     public float health;        
     public float score;            
 
     public Rigidbody rigidBody;    
 
     public Text scoreText;    
 
     
     void OnCollisionEnter(Collision collision)
     {
         
         if (collision.gameObject.tag == "Racer" || collision.gameObject.tag == "Player")
         {
             score += rigidBody.velocity.magnitude * 25;
             health -= rigidBody.velocity.magnitude * 2;
         }
 
 
     }
 
     void Update()
     {
         
         scoreText.text = score.ToString ("0");
 
         if (health <= 0)
         {
             health = 0;
             Destroy (gameObject, 0.5f);
 
             if (gameObject.tag == "Player")
             {
                 //call end of race here
             }
         }
     }
 }
 

What I need to do, once the player is dead, is retrieve the score float from all the objects in the scene that has the previous script attached to it. Here's my end race script, I'm pretty sure a lot of it is wrong.

 public Canvas raceEndCanvas;
 
     public HealthScoring scoring;
     public GameObject playerObj;
     public GameObject racer;
 
     public int[] scores;
 
     void Start()
     {
         scoring = GetComponent<HealthScoring>();
         racer = GameObject.FindGameObjectsWithTag ("Racer");
         playerObj = GameObject.FindGameObjectWithTag ("Player");
     }
 
     public void EndOfRace()
     {

       for (int i = 0; i < scores; i++)
     {
      // compare, sort scores
     }
     }


Now the problem I'm having is how do I get the individual score float from each Racer / Player game object in the scene, compare them and sort them highest to lowest? And do I need another array to store the number of Racer game objects in the scene?

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

1 Reply

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

Answer by Xarbrough · Jun 22, 2015 at 12:06 AM

Hm, first "racer" on line 5 is a GameObject, but should be an array GameObject[], because your looking for FindGameObjectsWithTag, which return an array.

Now you want to create a list or an array for all the scores you want to compare and then iterate over it or just add for each object that has a score.

Then, you can sort like this, for example: Array.Sort

However, I think it would be much easier to use a generic list and a foreach loop to .Add the scores.

Comment
Add comment · Show 3 · 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 Atzig · Jun 23, 2015 at 12:43 PM 0
Share

Yeah, a list might be an easier way to go, it just raises more questions for me though.

avatar image Xarbrough · Jun 23, 2015 at 04:47 PM 0
Share

Arrays

Lists and Dictionaries

All of those are so-called Collections, meaning that they store things in a sequence, which can be iterated through. Internally there is no or only a small difference between all collections. It mostly comes down to what syntax is helpful for you. If you want to be able to resize your collection, say add one element after another to it; a list is convenient.

avatar image Atzig · Jul 02, 2015 at 03:20 PM 0
Share
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class Scoring : $$anonymous$$onoBehaviour {
 
     List<GameObject> racerList = new List<GameObject>();
     public GameObject racer;
     public HealthScoring healthScoring;
     public float scores;
     public Text scoreText;
     public Text scoreBoardText;
     public int maxScores;
 
     void Start()
     {
 
         foreach(GameObject racer in GameObject.FindGameObjectsWithTag("Racer"))
         {
             racerList.Add(racer);
             racer.GetComponent<HealthScoring>();
             scores = racer.GetComponent<HealthScoring>().score;
             scoreText.text += scores.ToString("") + "\n" ;
         }
 
     }
 
     // Update is called once per frame
     void Update () {
 
         //scoreText.text = scores.ToString ("");
     }
 }
 
 

Had to delete my previous comment, had some weird errors. The previous code works fine but how can I update the scores float? If I add my own function GetScores(with the foreach code inside) and call that in Update, it just keeps adding entries and I can't seem to stop it.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Sorting a list by distance to an object? 1 Answer

Sort Multiple Arrays By Their Length 1 Answer

Matching Index of two Arrays after one Array is sort 3 Answers

javascript, default object comparator 1 Answer

Sorting array 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