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

2D array with GameObject and Float

Discussion in 'Scripting' started by Ben Norman, Oct 13, 2015.

  1. Ben Norman

    Ben Norman

    Joined:
    Sep 8, 2015
    Posts:
    9
    Hi people of Unit.

    I whould like to make an 2D array visible in the inspector so I can drag GameObjects in it(go#) and write a float value (relatieveFactor#) so I can use those later in the script.
    It should look like I show you below, but I ceep failing. I think I mix all the information a searched on the web.

    Objecten
    size
    element1
    go1
    relatieveFactor1​
    element2
    go2
    relatieveFactor2​
    ...​

    This is the code i've got for it:

    Code (CSharp):
    1. [System.Serializable]
    2. public class Objecten
    3. {
    4.     public GameObject element;
    5.     public float relatieveFactor;
    6. }
    7.  
    8.  
    9. public class CubeMaker : MonoBehaviour {
    10.     public Objecten[,] parts = new Objecten[GameObject ,float 1.0]; //default float = 1.0
    11.     ...
    Or do I need to use get and set for it?
    I hope you can help me and thanks for your time.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Unity doesn't serialize multi-dimensional arrays.

    So it's not going to show up in the editor.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Furthermore:
    Code (csharp):
    1.  
    2. public Objecten[,] parts =new Objecten[GameObject ,float1.0];
    3.  
    THAT is NOT how you create an array.

    I think you mean to have a one dimensional of a data structure that is made up of 2 distinct values. One typed GameObject, one typed float.

    Try something like this:

    Code (csharp):
    1.  
    2. public class CubeMaker : MonoBehaviour
    3. {
    4.     public Objecten[] parts;
    5. }
    6.  
    Objecten defines the two values for you, the array is just of those Objectens.

    Note, you'd set the default value for 'relativeFactor' in the class its in, but just as I brought in this other thread:
    http://forum.unity3d.com/threads/class-not-using-default-values-in-inspector.360820/

    default values don't work for array elements as you might expect... it's just the way the serializer works. So you're not going to get to default the factor to 1f quite the way you want.
     
  4. Ben Norman

    Ben Norman

    Joined:
    Sep 8, 2015
    Posts:
    9
    I'll try to work with your information.
    Thanks for your time and effort.