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 SwagGamez · Dec 03, 2015 at 06:44 AM · sorting

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?

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

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 SwagGamez · Dec 03, 2015 at 03:23 PM 0
Share

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.

avatar image SwagGamez · Dec 15, 2015 at 11:41 PM 0
Share

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.

avatar image toddisarockstar SwagGamez · Dec 17, 2015 at 01:43 AM 0
Share

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

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

32 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

Related Questions

help please. I wrote a mergesort algorithm that does nothing. 0 Answers

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

Transparency sorting using separate camera 0 Answers

Compare and sort Input 0 Answers

Sorting layers without far objects overlapping closer ones? 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