- Home /
Array Utility
Hello all,
I'm having some trouble understanding ArrayUtilities and how to use them properly. I have a good use for using ArrayUtility.FindIndex. If anyone can assist with this that would be great. I might be able to answer myself by the end of the night. Thanks in advance.
static function FindIndex. (array : T[], match : Predicate) : int
Array.Add
function Add (value : object) : void
ArrayUtility.Add
static function Add. (ref array : T[], item : T) : void
my code example
using UnityEngine;
using System.Collections;
public class gameScript : MonoBehaviour {
private GameObject fallSpawn;
public Transform fallObj;
public bool isSpawning = false;
// Use this for initialization
void Start () {
spawnFallObj(isSpawning);
}
public void spawnFallObj(bool isSpawning){
var go = GameObject.FindGameObjectsWithTag("fallSpawn");
if(!isSpawning){
isSpawning = true;
foreach(GameObject fallgo in go){
Instantiate(fallObj, fallgo.transform.position, Quaternion.identity);
if(FindIndex.<go>(array:go[],match: go) {//<< FindIndex //gameObject of go array of GameObjects
isSpawning = false;
}
}
}
}
// Update is called once per frame
void Update () {
}
}
I'm basically want to only call a function only after the last gameObject has been spawned.
strongly suggest you just use List, search on here for 100s of examples.
Answer by hoy_smallfry · Mar 22, 2013 at 06:25 AM
ArrayUtility is a Editor class, which means it can not be used to make scripts for the game.
Fortunately, you can use System.Array.FindIndex(). This function is a static function, as well as a C# generic. If you aren't familiar with either concepts, I suggest you read both articles I've linked for a more details.
Generics let programmers write code where certain types can be defined later, like a fill-in-the-blank.
Example:
T GetAtIndex<T>(T[] pArray, int i)
{
return pArray[i];
}
Since it is a generic function, when it is used, you have to specify what kind of type T
will be:
int[] myArray1 = {1, 2, 4, 0, 23};
int[] myArray2 = {false, false, false, true};
int element1 = GetIndex<int>(myArray1, 2); // 4
bool element2 GetIndex<bool>(myArray2, 3); // true
For the first one, all T
's are replaced with int
, and for the second, all T
's are replaced with bool
. Whatever replaces the T
must be a type, such as GameObject
, Component
, int
, bool[]
, etc. In some cases, you don't even have to specify what T is - the compiler will figure that out for you:
// decides that T is an int because of myArray1's type
int element1 = GetIndex(myArray1, 2);
// decides that T is a bool, same reason
bool element2 GetIndex(myArray2, 3); // true
Now, back to System.Array.FindIndex()
. According to the MSDN documentation of it, you give it a T[]
for the array and a System.Predicate
for the match function. Predicate
is a delegate, which is a variable that can store a reference any function that follows a pattern. The pattern for Predicate
is:
bool functionName<T>(T pValue);
When FindIndex()
goes through each element in the array, it calls the predicate function and passes the element in. If predicate then returns true, FindIndex will return the index for that element. If the predicate returns false for all the elements, FindIndex()
returns -1 instead. So, the predicate is the most important part of how FindIndex()
works, because whichever function you pass in as the predicate decides what index gets returned.
Here's an example of how it all works:
// the location of FindIndex<T> and Predicate<T>
using System;
// ...
bool MatchFunction1(string arrayElement)
{
// returns true if the element matches "GHI"
return arrayElement == "GHI";
}
bool MatchFunction2(string arrayElement)
{
// returns true if the element matches "MNO"
return arrayElement == "MNO";
}
// ...
string[] myArray = { "ABC", "DEF", "GHI", "JKL" };
// index will be 2
int index = Array.FindIndex(myArray, MatchFunction1);
// no match found, index will be -1
int index = Array.FindIndex(myArray, MatchFunction2);
@jbarba-ballytech, now when I take a good look at ArrayUtility, I found out that I mistakenly take ArrayUtility as System.Array.
Anyway, that's a very detailed and complete answer.
No problem. I noticed that this still doesn't solve @$$anonymous$$nky's problem.
$$anonymous$$nky, you want to be able to know when the last GameObject is created, right?
One way to do that is to make a script that takes a delegate. The Predicate I explained earlier is also a delegate; these are variables that can hold references to a function, as long as the return type and arguments match the delegate. We can then call the delegate in Start() if its not null:
using UnityEngine;
using System.Collections;
public class AwakeCallback : $$anonymous$$onoBehaviour
{
// any function that takes a game object and returns nothing
public delegate void Callback(GameObject pObject);
public Callback callback = null;
void Start()
{
// pass in this game object
if(callback != null)
callback(gameObject);
}
}
Then, in the script you posted, write a function that the delegate will be set to:
void onInstantiate(GameObject pObject)
{
Debug.Log (pObject + " instantiated! Let's do something in here!");
}your first script, attach this function to the last object:
Then, do this with the the for loop:
GameObject lastObject = null;
foreach(GameObject fallgo in go)
{
//saves the current game object as the last one.
lastObject = Instantiate(fallObj, fallgo.transform.position, Quaternion.identity); as GameObject;
}
// attach the script we made.
var script = lastObject.AddComponent<AwakeCallback>();
// set the delegate.
script.callback = onInstantiate;
Answer by Mnky · Mar 23, 2013 at 07:23 AM
I appreciate the input from everyone. jbarba_ballytech you gave alot of great information and your time and effort is greatly appreciated. When I get the chance I'll post my results back here and tell you all how my progress goes. Thanks again all.
Your answer
Follow this Question
Related Questions
Array Utility error while building project 1 Answer
how subtract total value element of array ?? 1 Answer
Random textures... 1 Answer
How can I make an NPC follow a path specified by an array? 0 Answers
Array.length edited using scripts 2 Answers