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 JChiu · Sep 12, 2013 at 07:51 AM · c#arrayssortingget component

How to sort get components?

I have a game object that I have set it to travel along some pathway points. They are named as follows: Path01 to Path15. When I was first implementing them and playing them the orders goes like this Path01, Path02, Path03, etc. However, once I restarted Unity 4, the whole order is messed up, now every time is something like these Path04, Path12, Path09 and hope you get the point. Thus, is messing up the way I want that game object to travel. How can I solve this as I have sought for answers to this problem but none fully addressed the way I implemented?

Here is my code:

Public GameObject[] Path;

void Start(){ path = GameObject.FindGameObjectsWithTag("path"); }

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fffMalzbier · Sep 12, 2013 at 09:38 AM

Its much more efficient to make an public Array of Transforms that you can fill with the waypoints in the editor.

If the waypoints not exist at scene startup you should use there names to identify them.

             Transform tmptransform;
             Transform []path = new Transform[20];
             for(int i = 0; i< path.Length;i++)
             {
             
                 tmptransform = GameObject.Find("WP" + i);
                 if(tmptransform != null)
                 {
                     path[i] = tmptransform;
                 }
             }
Comment
Add comment · Show 1 · 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 JChiu · Sep 13, 2013 at 08:07 AM 0
Share

As you can see I have re-posted, basically the for loop not going to work. Alternative?

avatar image
0

Answer by Nexum1 · Sep 12, 2013 at 09:43 AM

FindGameObjectsWithTag unfortunately does not guarantee order. You can however name your objects numerically and simply bubble sort the array returned by FindGameObjectsWithTag and then just use Path[i].name to sort by.

Look at How to bubble sort if you don't already know bubble sorting...

Alternatively you can try these two functions to sort (C#), but again, it's according to the game object's name..

 GameObject[] FindObsWithTag( string tag )
 {
   GameObject[] foundObs = GameObject.FindGameObjectsWithTag(tag);
   Array.Sort( foundObs, CompareObNames );
   return foundObs;
 }
  
 int CompareObNames( GameObject x, GameObject y )
 {
   return x.name.CompareTo( y.name );
 }
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
avatar image
0

Answer by JChiu · Sep 13, 2013 at 02:46 AM

I should have post the entire script instead of just the parts I think is responsible as the above suggestions create more headache as how my entire script is structured.

using UnityEngine; using System.Collections;

public class Lv1BossAI : MonoBehaviour {

 public Transform pathway;
 public GameObject[] path;
 public float speed;
 public float rotate;
 public int currentPath; //When the trigger enter a path point it change this varible then pass it on to ToPoint. 
 public Transform emitPoint;
 public GameObject closeAtt;
 public GameObject farAtt;
 public GameObject deathReplace;
 
 private weakPointHealth wph;
 private Transform player;
 private Transform own;

 // Use this for initialization
 void Start () {
     GameObject p;
     own = transform;
     //Find all paths
     path = GameObject.FindGameObjectsWithTag("path");
     p = GameObject.FindGameObjectWithTag("Player");
     player = p.transform;
     
     InvokeRepeating("Far", 1, 30);
     InvokeRepeating("Close", 1, 6);
     
     
 }
 
 // Update is called once per frame
 void Update () {
     GameObject death;
     
     //If the script are not found kill the object.
     wph = GetComponentInChildren<weakPointHealth>();
     if (wph == null) {
         death = Instantiate(deathReplace, own.position, own.rotation) as GameObject;
         death.SetActive(true);
         Destroy(gameObject);
     }
     
     ToPoint(currentPath);
     
 }
 
 //Move to the next point
 void ToPoint (int pathArr){
     
     pathway = path[pathArr].transform;    
     
     own.rotation = Quaternion.Slerp(own.rotation, Quaternion.LookRotation(pathway.position - own.position), Time.time * rotate);
     own.position += own.forward * speed * Time.deltaTime;
     
 }
 
 void OnTriggerEnter (Collider collider) {
     
     if (collider.gameObject.tag == "path") {
       
         //Register the next point
         if (currentPath != 15) {
             currentPath++;
         }
         
         
         ToPoint(currentPath);
         
     }
 }
 
 //How far is from the player
 void Close() {
     GameObject cloneC;
     
     
     float Dist = Vector3.Distance(player.position, own.position);
     
     //when player is near
     if (Dist < 20) {
         cloneC = Instantiate(closeAtt, emitPoint.position, emitPoint.rotation) as GameObject;
         cloneC.SetActive(true);
     }
 }
 
 void Far() {
     GameObject cloneF;
     
     float Dist = Vector3.Distance(player.position, own.position);
     
     //when player is near
     if (Dist > 20) {
         cloneF = Instantiate(farAtt, emitPoint.position, emitPoint.rotation) as GameObject;
         cloneF.SetActive(true);
     }
 }
 
 
 

}

Comment
Add comment · Show 2 · 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 fffMalzbier · Sep 13, 2013 at 07:59 AM 0
Share

You should have been using the Edit function and not posting a new answer.

avatar image JChiu · Sep 13, 2013 at 08:05 AM 0
Share

And you should aid me ins$$anonymous$$d of just writing such a trivia sentence.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Sorting Collider Arrays by location of elements 1 Answer

Distribute terrain in zones 3 Answers

Vector3 resultant array sorting 2 Answers

MergeSort function acting strange 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