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 Mnky · Mar 22, 2013 at 03:37 AM · arrayutility

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.

Comment
Add comment · Show 1
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 Fattie · Mar 22, 2013 at 08:02 AM 0
Share

strongly suggest you just use List, search on here for 100s of examples.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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);

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 Chronos-L · Mar 22, 2013 at 06:35 AM 0
Share

@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.

avatar image hoy_smallfry · Mar 22, 2013 at 07:09 AM 0
Share

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;




avatar image
0

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.

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

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

14 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

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


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