how do I Instantiate gameobject and add to array and then move the previous instantiated gameobject above the current?
I created a UI text prefab and instantiated it into an array. I want to move the previous instantiated object above the current one. I'm still learning programming and this one is holding me off. Any help will be greatly appreciated. See below for the details.
public class DisplayLogs : MonoBehaviour {
public GameObject textLogs;
GameObject[] logtext;
int arrayPosition = 0;
void Awake()
{
logtext = new GameObject[6];
arrayPosition = 0;
}
public void addLog(string text)
{
textLogs.GetComponent<Text>().text = text;
print("ArrayPosition: " + arrayPosition);
logtext[arrayPosition] = Instantiate(textLogs).gameObject;
RectTransform tempRect = logtext[arrayPosition].GetComponent<RectTransform>();
logtext[arrayPosition].transform.SetParent(this.transform);
tempRect.transform.localPosition = textLogs.transform.localPosition;
tempRect.transform.localPosition = new Vector3(textLogs.transform.localPosition.x, textLogs.transform.localPosition.y + (arrayPosition * 15), textLogs.transform.localPosition.z);
tempRect.transform.localScale = textLogs.transform.localScale;
tempRect.transform.localRotation = textLogs.transform.localRotation;
Destroy(logtext[arrayPosition].gameObject, 5f);
arrayPosition = arrayPosition + 1;
if (arrayPosition == 6)
{
arrayPosition = 0;
}
}
}
Answer by Dibbie · Nov 11, 2015 at 03:27 PM
You could do it in 2 ways:
You could reload your array every time you add a new index:
void ArrayFunction{ public GameObject[] arr; //Default the size to a size of 1 arr = new GameObject[1];
//Now your code to update the array, however you are doing it, heres one method
InvokeRepeating("AddOnetoArray",3f,3f); //Adds an item to the array every 3 seconds.
}
void AddOnetoArray(){
//clone array before changing it, store it in a temp variable
GameObject[] tempArr;
tempArr = arr;
//Update the array
arr = new GameObject[arr.length]; //Since we set it to one before, it will read index 0 as 1, so this will be 2.
//Instantiate a new gameobject, and give it a variable name
GameObject bob = (GameObject) Instantiate(yourPrefab,yourPrefabPos,yourPrefabRot); //Spawn your actual object
//Load your actual object into your array
arr[0] = bob;
//The old array has just been overloaded, but we still have tempArr with the old data, so now lets load it back in
for(int i = 1; i < tempArr.length - 1; i++){ //Start at 1, so we never overright the 0th index, being the most recent addition
arr[i] = tempArr[i - 1]; //i - 1 so that it starts at 0, since we start at 1.
}
}
You could also sort it automatically:
void ArrayFunction{ public GameObject[] arr; public bool sortArray; //Default the size to a size of 1 arr = new GameObject[1];
//Now your code to update the array, however you are doing it, heres one method InvokeRepeating("AddOnetoArray",3f,3f); //Adds an item to the array every 3 seconds. } void AddOnetoArray(){ //clone array before changing it, store it in a temp variable GameObject[] tempArr; tempArr = arr; //Update the array arr = new GameObject[arr.length]; //Since we set it to one before, it will read index 0 as 1, so this will be 2. //Instantiate a new gameobject, and give it a variable name GameObject bob = (GameObject) Instantiate(yourPrefab,yourPrefabPos,yourPrefabRot); //Spawn your actual object //Give bob a specific name, adding its index location as an identifier bob.name = "Cubical " + arr.length - 1; //Load your actual object into your array arr[0] = bob; //Run a function that sorts out the array for us. You could also move this functions code into Update instead, which may be more accurate (quicker) but can eventually get laggy when you have something like a 200+ array. sortArray = true; SortThisOut(); } } public void SortThisOut(){
if(sortArray == true){ //Copy the array for a second in a temp array, case we mess up GameObject[] tempArr = arr; for(int i = 0; i < arr.length; i++){ if(tempArr[tempArr.length - i].name.Last(i)){ //If the last gameobject in the array ends with the current number we are on... arr[i] = tempArr[tempArr.length - i]; } } sortArray = false; } }
The second method is a much more simplified version of "bubble sorting", which using bubble sorting is I personally find annoying... But its very efficient, so that is also an option.
All this code is untested by the way... So if you run into any problems, I can help you sort (no pun intended) through it.
This is ok but this is not what I want. I don't need InvokeRepeating since i don't want to automatically instantiate UItext.
I have an ammo prefab on the floor and when i pick it up the function addLog will be called thus instantiating the text that will display what i picked up. I want to add the instantiated text to an array so that the first ammo that i picked will be instantiatedtextammo[0] and then the second will be instantiatedtextammo[1] and so on and so forth. When I pick the next ammo, I want to move the previous instantiatedtextammo above the current instantiatedtextammo.
@bakill15 Invoking is just one method, I used as an example to show you how the code works, you really should replace that line with your actual code your using to instantiate your objects, weather its invoking, or a function call or right from there. Sorry I didnt make that more clear.
Right, and doing it in this method, should produce that, because what you need to do is SORT your list, re-order it every time its changed/updated, which is what either method tries to achieve.
Though if its just UI Text, and not actual game object prefabs, why not use an array of Text? Theres no real harm in using an array of GameObjects, it just makes more sense to me, to keep it all the same, but that could be the inner-perfectionist in me trying to come out haha.
I tried it, the arr.Length stays at 1 after "update the array" so the for loop doesn't execute.