Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to get array value as an variable C#

Discussion in 'Scripting' started by Leonard-Walker, Jul 14, 2015.

  1. Leonard-Walker

    Leonard-Walker

    Joined:
    Oct 31, 2012
    Posts:
    14
    Code (CSharp):
    1. public MyTransforms[] splitter= new MyTransforms[2];
    How do I get element 0 and element 1, So that I can assign them to in a custom editor.

    using
    EditorGUI.PropertyField
    and
    EditorGUI.LabelField
     
  2. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    489
    Define separate properties for each of the array elements and assign those to the EditorGUI property fields? I've not worked with a custom editor GUI myself yet, but briefly looking at the documentation it doesn't seem like you can refer to members of an array directly.

    Code (CSharp):
    1. public MyTransforms Splitter0 { get { return splitter[0]; } set { splitter[0] = value; }}
    Edit: actually, you need to use SerializedProperties; you should probably follow a tutorial on how to use these if the documentation doesn't help you.
     
    Last edited: Jul 14, 2015
  3. Leonard-Walker

    Leonard-Walker

    Joined:
    Oct 31, 2012
    Posts:
    14
    I am unsure of how to do that, can you show an example of how you would do so?