Trying to sort a list of variables. Position in car racing game.
JavaScript please. I would like to sort and display gameobjects names from highest to lowest according to variables in their respective scripts. Its actually a list of cars sorted by their position in the race so maybe I can sort the variables and display which position I am in according to my rank in the list. I dont know how to create lists or arrays nor do I know how to use compare or sort functions. Please help!!! It is the last thing to create for my game to be complete.
For example:
"GameObject1" has a script called "Script1" with a variable called "Condition1"
"GameObject2" has a script called "Script2" with a variable called "Condition2"
There 8 GameObjects. I would like to sort the gameobjects from highest to lowest and display the list of objects on screen and watch the list change on screen according to the variables. Is this possible?
Answer by toddisarockstar · Dec 03, 2015 at 08:24 AM
well first of all i would assume your game has a master script attached to something neutral other than the cars. if not, you will need one at somepoint before you finish.
so lets assume you have a javascript attached to the camera called "masterscript". and then the cars could report positions or variable to that script for calculation.
so each cars individual script would say at the top above update something like this:
var cam:GameObject;
cam=gameObject.Find("Main Camera");
var mscript: level = cam.GetComponent(level);
mscript = cam.GetComponent(level);
obviosly each car should have a variable in its script to somehow know what car number it is. var carnumber:int;
if(transform.name=="object0"){carnumber=0;}
if(transform.name=="object1"){carnumber=1;}
if(transform.name=="object2"){carnumber=2;}
set up an array in the masterscript to recieve car positions. if you know you always have eight car its as simple as this:
var carpositions:float[];
carpositions=new float[8];
now that you have this set up, each car reports stuff into the array on the master script with this:
var distancefromstartline:float;
mscript.carpositions[carnumber]=distancefromstartline;
now if you're still following me you should have a happy array of all the car positions neatly packed into one script to display or do whatever.
arrays are really cool and are very handy
there are many ways to do it but here is an example of pageing through the array to find your highest car!
var winningcarnumber:int;
var f:float;
var i:int;
i=carpositions.Length;
f=0;
while(i>0){i=i-1;
if(carpositions[i]>f){f=carpositions[i];
winningcarnumber=i;}
}
keep in mind that if you have 8 cars. the first car is zero. your array is 0-7; there is not an 8. its best to name them all accordingly to keep things uniform in your project. hope this all helps to get you thinking in the right direction. your masterscript could simply display the winning car number on screen or even all positions or whatever. that's another question!
Thank you for your quick and thorough response. Im a little baffled on a few parts but when I get home and try Im sure Ill have a few more questions. Im sure once I get my epiphany Itll be so clear to me but Im still learning scripting.
Ok Im pretty stumped. Im not sure how to put the scripts together. Like which function to put them under, not sure about the "level" reference. I usually learn by using reverse engineering on other scripts.
sorry for the delay getting back to you. "level" was just the assumed name of a new script. it could be anything you name a script you wanted to lookup. the code examples i gave should work anywhere in the script. i usually put my lookups above Update. the code i gave is a simple example to get you started to make one script talk to another. i got the impression that it would be an inevitable necessity before you game is finished.
the example shows how to make the car scripts change variables in a another central script.
i would assume some sort of single central script would be helpfull to you anyways for things like menus and display and camera movement or whatever.
Sorry for the lack of explaination. the first block of code example assumes you have attached a new script to your main camera that you have named "level".
you could replace "$$anonymous$$ain Camera" with any object where a script attached and replace "level" with the name of any script attached and it works the same.
after the first code example you can easily change variables in another script simply by saying mscript. first.
mscript.whatevervariableintheotherscript=5;
arrays would be handy in your game. actually they are essiential to learn to do a lot of things in game devolopment. but really its a seperate issue than getting scripts to talk. i would research and understand them individually before your head eplodes! LOL.
theoretically, with arrays you could be driving all your cars from just one script. actually sometimes thats easier.
Your answer
![](https://koobas.hobune.stream/wayback/20220612051225im_/https://answers.unity.com/themes/thub/images/avi.jpg)