Problems with For Loop in C# (Unity new UI).

Hey everyone,

I have a problem with a code I’ve written to sort images from the new UI system in a scoreboard-like list. The images all have a number value of which number they are of all added images (eg. the first image ever is 0, the one created after it is 1, etc.) and a score value, which I want to use for the sorting.

So far, this is my code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class scoreboard : MonoBehaviour {

	float myValue;
	float myScore;
	float oldY;

	void Start () {
		myScore = float.Parse (transform.GetChild (1).transform.GetChild (0).GetComponent<Text> ().text);
		myValue = float.Parse (transform.name.Substring(24));
		transform.localPosition = new Vector3(-10f, -780f + 64 * (myScore - 1f), 0f);
	}

	void Update () {
		myScore = float.Parse (transform.GetChild (1).transform.GetChild (0).GetComponent<Text> ().text);
		myValue = float.Parse (transform.name.Substring(transform.name.Length-1));
		for(int i = 0; i <= myValue-1; i+=1){
			GameObject newGO = GameObject.Find("StudentDataPrefab(Clone)" + (i).ToString());
			if(transform.localPosition.y == newGO.transform.localPosition.y){
				transform.localPosition = new Vector3(newGO.transform.localPosition.x + 60f, -780f + 64 * (myScore - 1f), 0f);
			}
		}
		if (oldY != transform.localPosition.y) {
			oldY = transform.localPosition.y;
			transform.localPosition = new Vector3(-10f, -780f + 64 * (myScore - 1f), 0f);
		}
	}
}

I don’t know what’s wrong here, because it works with the first 9 images in the list, but the vertical sorting stops working from the 10th image.

I hope someone can help me here!

I fixed it!

Just in case anyone wonders what I did wrong, I was setting the y-value only when it was equal to another y value, causing it to work when there were two or more images next to each other, but no longer afterwards because no two images were on the same y-value.

Here’s my new code!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class scoreboard : MonoBehaviour {

	float myValue;
	float myScore;
	float newY;

	void Start () {
		myScore = float.Parse (transform.GetChild (1).transform.GetChild (0).GetComponent<Text> ().text);
		myValue = float.Parse (transform.name.Substring(24));
		transform.localPosition = new Vector3(-10f, -780f + 64 * (myScore - 1f), 0f);
	}

	void Update () {
		myScore = float.Parse (transform.GetChild (1).transform.GetChild (0).GetComponent<Text> ().text);
		myValue = float.Parse (transform.name.Substring(transform.name.Length-1));
		newY = -780f + 64 * (myScore - 1f);
		transform.localPosition = new Vector3(-10f, newY, 0f);
		for(int i = 0; i <= myValue-1; i+=1){
			GameObject newGO = GameObject.Find("StudentDataPrefab(Clone)" + (i).ToString());
			if(transform.localPosition.y == newGO.transform.localPosition.y){
				transform.localPosition = new Vector3(newGO.transform.localPosition.x + 60f, newY, 0f);
			}
		}
		Debug.Log ("Leerling "+ myValue.ToString() + ": -780f + 64 * (" + myScore.ToString() + " - 1f) = " + transform.localPosition.y.ToString());
	}
}