- Home /
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"); }
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;
}
}
As you can see I have re-posted, basically the for loop not going to work. Alternative?
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 );
}
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);
}
}
}
You should have been using the Edit function and not posting a new answer.
And you should aid me ins$$anonymous$$d of just writing such a trivia sentence.
Your answer
Follow this Question
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