2
\$\begingroup\$

I am fairly new to Unity 5, as I was browsing through some prefabs, I found one for the Main menu in which UnityEvent was used in scripts to list down the menu options.

This UnityEvent was an array UnityEvent [ ] Event, which was nowhere initialized to anything but was showing no compiler errors and also during run, it was pointing to some predefined sets which the author was mentioning in the user manual... My questions are :

  • Is it possible to use UnityEvent Array without mentioning the size of the array.

  • What happens to the UnityEvent if we don't mention any of the events, but the Invoke functionality is called during runtime.

Btw, the prefab i used from was :Main Menu with Parallax effects

\$\endgroup\$
1
  • 1
    \$\begingroup\$ If a public variable isn't initialized it will once you create an instance of the component in the editor. So basically the array will be instantiated, but the length is zero. \$\endgroup\$ Commented Oct 11, 2017 at 14:22

1 Answer 1

1
\$\begingroup\$

To complete my comment:

  1. If a public variable isn't initialized it will once you create an instance of the component in the editor. So basically the array will be instantiated, but the length is zero. Unity does the same for Lists.

  2. The array is most likely used in conjunction with a for loop. If your array is of length 0, the for loop exits before it even enters the loop body.

The following is completely valid:

public class Test : MonoBehaviour {

public int[] myArray; // On adding this component myArray gets initialized 

// Update is called once per frame

void Update () {

    //myArray.Length is zero, so the Debug.Log is never run

    for(int i = 0; i < myArray.Length; i++){
        Debug.Log(myArray[i]);
    }
  }
}
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.