Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by thomasmahler · Mar 05, 2015 at 11:10 AM · arrayvariablesfindsort

Find gameObject with higher variable

Hey guys,

I'm still kinda wondering about this. I'm currently working on something like a heat seeking missile (similar to a red shell in Mario Kart).

So, at all times, there are 2-4 cars on the track. Every one of those cars has a 'carSettings' component with a 'rank' variable that shows their current position.

So, say I'm in 2nd position and I'm firing the heat-seeking missile: Obviously, it should find the player in the first position. But that's where I'm stumbling, code-wise.

I think I'd need to create another array that has all the cars on the track in it and then sort it through the rank variables of all cars - And I know how to do that.

But then how would I find the next higher rank?

Here's what I'm doing right now:

     if(Input.GetButtonDown(LBButton))
     {
         if(hasRedShell)
         {
             findTarget();
             var spawnedRedShell : GameObject = Instantiate(Resources.Load("Prefabs/redShell"), itemSpawnPosFront.transform.position, itemSpawnPosFront.transform.rotation);
             var spawnedRedShellScript = spawnedRedShell.GetComponent(redShell);
             spawnedRedShellScript.target = target;
         }

And this works nicely. Now, the issue lies in finding the target, which should be the next 'lower' rank, so that the target becomes the racer in 1st place when I'm in 2nd place. I tried this now:

 function findTarget()
 {
     getPlayers = GameObject.FindGameObjectsWithTag("Player"); //Find all cars:
     var carP1 = getPlayers[1];
     var carP2 = getPlayers[0];
     
     var compareRanks = [carP2.GetComponent(carSettings).rank, carP1.GetComponent(carSettings).rank]; //Compare their ranks
 
     System.Array.Sort(compareRanks, getPlayers); //Sort the arrays, so they're connected:
 
     target = getPlayers[0];
 }

Which always gives me the highest rank... but obviously, I'd just need to find the NEXT HIGHER rank based on the rank variable in this gameObject.

I hope that made sense - I'm kinda stumbling over the logic and over how to make that query...

Comment
Add comment · Show 3
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 hexagonius · Mar 05, 2015 at 07:55 PM 0
Share

Is the red shell only supposed to work when you're in second place? Assu$$anonymous$$g that's not the case, it's a writing mistake that you said 'lower' rank AND the script is a component of the player in question, you could write something like this (C# code) :

 void findTarget()
  {
      getPlayers = GameObject.FindGameObjectsWithTag("Player"); //Find all cars:
      carSettings self = GetComponent<carSettings>();
      foreach (var p in getPlayers){
        if (p.GetComponent<carSettings>() == self) continue;
        if (p.GetComponent<carSettings>().rank == self.rank +1) target = p;
 }
avatar image DomantasB · Mar 05, 2015 at 08:21 PM 0
Share

If I understand you correctly you just want to find car which is only one position(ranks) higher then you and set that as your missle target? Then after you sort your array get your car index and car one posion higher is just your index + 1. Please correct me if I'am wrong

avatar image SwagGamez · May 26, 2015 at 03:32 PM 0
Share

Hey guys, I've recently switched from GameSalad to Unity so I'm still learning. Can I ask you how did you achieve your ranking method/system? I'm making a car game and that's the last thing I need to complete it. I've researched and tried everything. Can anyone help? I can post any info if anyone is interested in helping me. Thanx in advance.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jaja1 · May 27, 2015 at 12:41 AM

Hope I understood the question correctly. I like to keep things as simple as possible. So here is just a suggestion (and some pseudo code):

First, create a class that constantly checks for the "rank" of your cars using say, a vector check on Update(). This class should also contain a static collection of all the carSettings components in the game. This is a good time to utilize constructors. When the carSettings component is instantiated with its gameobject, the constructor should add its class to the static collection like so:

 public carSettings()
 {
 
 RankChecker.AllCarSettings.Add(this);
 
 }

When I say static collection I mean a static Dictionary, List or Hashtable. I prefer these over arrays in most cases because they are "re-sizable". So if you decide to switch to 10 cars instead of 4 its not an issue.

Second, when your "missile" is launched you no longer have to "find" the carSettings components because you can simply call the static collection of the previously mentioned class. Then, you can use a foreach loop to "seek" the correct target from the carSettings class (or any other class) because the collection is static:

 int myCarRank;//rank of THIS car
 
 foreach(carSettings c in RankChecker.AllCarSettings/*this is the "collection"*/)
 {
    if(myCarRank != 1)//if we are ahead no need to fire missiles ;)
    {
       if(c.myCarRank == myCarRank + 1) 
       FireMissile();//of course you will implement how the missile's       //position is determined
    }
 }

This works well because there should only be ONE car in the next rank above our own if the rank check is implemented correctly. That is, there should not be two cars in second place simultaneously while we are in third. This sort of idea with the static collection should be used in place of GameObject.Find because it is faster. Assign all your members to the collection ahead of time so you do not have to search for them later.

Also, sorry for the poor formatting. I did not type this up in an editor.

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

6 People are following this question.

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

Related Questions

C# - How do I access a variable on a passed Type? 2 Answers

Vector2 array sort clockwise 3 Answers

Storing a variable and it's name 3 Answers

Custom Sort class array error if i use #pragma 2 Answers

Ordering an array of GO based on interfaced variables using linq. 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