Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by RGM-Red_Dragon · Nov 07, 2018 at 12:45 PM · scripting problemprogrammingscoresortingsearch

Help to Add some Sort and Binary Search in the code.

Hello Budies,

I'm doing a job for school purpose, and then it was required into the Game i'm creating with a friend to use some sort by Ascending the points in a Rank every time the Space Ship destroy a target and Update the Score, and after this happen, implement the method binary search(recursive) or even a new method of binary search and return the exact position of the actual points of the player in the Rank. But i tried to insert the Method into the Update, but every time the position returned is -1. Besides that, an important thing to say is 2 things: First, the Sort by Ascending and Binary Search need to be done manually with every if, else, for, etc to do the sort and the search, this is one important requirement that i have been asked to do, sooo, nothing of array.sort() methods equals that i have done to test into the code, or ready unity methods to do the thing in just one line of comand. And second and more important, i'm trying everyday to seek and learn solutions to move the elements in and array to use conditional cases and binary search, but i assume that to me (who study arts and have a basic programming logic to understand how the codes work), was and yet still hard to understand how and why the codes to do this things works, and i'm doing my best to understand and learn about it, and all this reasons that i have pointed out above are the main reasons that i ask the help of you guys, because i'm think that this two implementations can be easy for someone who have more knowledge in this than me and my partner (an artist too). Now, follow the code: using System; using System.Linq; using System.Text; using System.Threading; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;

 public class GameController : MonoBehaviour
 {
 
     public GameObject Hazard;
     public Vector3 SpawnValues;
     public int HazardCount;
     public float SpawnWait;
     public float StartWait;
     public float WaveWait;
     public GUIText ScoreText;
     private int Score;
     public GUIText RestartText;
     public GUIText GameOverText;
     public bool GameOver;
     private bool Restart;
     public List<int> Lista_Array;
     public int[] arr = { 0, 1, 2, 3, 4, 5 };
 
     IEnumerator SpawnWaves()
     {
 
         yield return new WaitForSeconds(StartWait);
         while (true)
         {
             for (int i = 0; i < HazardCount; i++)
             {
                 Vector3 SpawnPosition = new Vector3(UnityEngine.Random.Range(-SpawnValues.x, SpawnValues.x), SpawnValues.y, SpawnValues.z);
                 Quaternion SpawnRotation = Quaternion.identity;
                 Instantiate(Hazard, SpawnPosition, SpawnRotation);
                 yield return new WaitForSeconds(SpawnWait);
             }
             yield return new WaitForSeconds(WaveWait);
 
             if (GameOver)
             {
                 RestartText.text = "Press 'B' to return to main menu or 'R' to restart";
                 Restart = true;
                 break;
             }
         }
 
     }
 
     public void UpdateScore()
     {
         ScoreText.text = "Score: " + Score;
         for (int i = 0; i < 1; i++)
         {
             //Lista_Array = new List<int>(Score);
             Lista_Array.Insert(0, Score);
             Lista_Array.Insert(0, 0);
             Lista_Array.Insert(0, 999);
             Lista_Array.Insert(0, 1999);
             Lista_Array.Insert(0, 2999);
             Lista_Array.Insert(0, 3999);
             arr = Lista_Array.ToArray();
             Lista_Array = new List<int>(Score);
             Array.Sort(arr); // Need to implement here manually some kind of sort by Ascending the Points in the Rank (Rank = Array and Points = Elements and Position = Position of Elements in the Array)
         }
     }
 
     public int BuscaBinaria(int[] arr, int l, int r, int x) //This is the Method of a Recursive Binary Search, but always return the position of -1
     {
         if (r >= 1)
         {
             int mid = 1 + (r - l) / 2;
             if (arr[mid] == x)
             {
                 return mid;
             }
             if (arr[mid] > x)
             {
                 return BuscaBinaria(arr, 1, mid - 1, x);
             }
             return BuscaBinaria(arr, mid + 1, r, x);
         }
         return -1; //Aqui era -1
     }
 
 
     public void AddScore(int NewScoreValue)
     {
         Score += NewScoreValue;
         UpdateScore();
         int n = arr.Length;
         int x = 10;
         int result = BuscaBinaria(arr, 0, n - 1, x);
         if (result == -1)
         {
             Debug.Log("Posicao nao encontrada");
         }
         else
         {
             Debug.Log("Posicao encontrada no Ranking " + result);
         }
     }
 
     public void gameOver()
     {
         GameOverText.text = "Game Over";
         GameOver = true;
     }
 
     public void Start()
     {
         GameOver = false;
         Restart = false;
         RestartText.text = "";
         GameOverText.text = "";
         Score = 0;
         StartCoroutine(SpawnWaves());
         UpdateScore();
     }
 
     void Update()
     {
         if(Restart){
             if(Input.GetKeyDown (KeyCode.B)){
                 SceneManager.LoadScene ("Menu");
             }
         }
 
         if(Restart){
             if(Input.GetKeyDown (KeyCode.R)){
                 Application.LoadLevel (Application.loadedLevel);
             }
         }
 
     }
 
 }
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

Answer by RGM-Red_Dragon · Dec 24, 2018 at 10:03 PM

Thank all the Community, the answer for this question is in the following link: Stack Overflow (https://stackoverflow.com/a/53917939/10618418) Thank you all

Comment
Add comment · 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

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

237 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 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 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

How can I use the same script for multiple gameobjects? 1 Answer

always the same problem, how to add 1 valor correctly not overtime ? 0 Answers

im trying to make it so when the Stamina reaches below 5, then the shift key will be disabled and when it is above 5, it will enable the shift key. 0 Answers

Moving script wont even move enemy no more? 0 Answers

A more optimized way to adjust duplicate integer values In a list? 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