jeff
jeff

Reputation: 39

Getting the length of an array

I have the following code where I can't get the length of the array The array size is defined in the unity inspector. The error is Error Item' does not contain a definition forLength'

public class Item : System.Object
{
    public string name;
    public int Radius = 1;
    public GameObject obj;
}

public class Gen : MonoBehaviour {
    public Item[] Items;
    int iNum = Items.Length; // gives me an error
}

I feel like the issue is staring me in the face but it's one of those days ....

Upvotes: 1

Views: 1085

Answers (6)

Chuck Savage
Chuck Savage

Reputation: 11955

You need to give the Items array variable an array. Your only defining a variable Items that holds an array of Item's, but actually holds nothing.

Also, you can't call Items.Length in class variable declarations. As Programmer has said, it must be done in a method or property. Since it is runtime code. So your Gen class minimally should look like:

public class Gen : MonoBehaviour 
{
    public Item[] Items = new Item[] { }; // Now Items has an array
    int iNum;

    void Awake()
    {  
        iNum = Items.Length; // iNum will in this case will be zero, because the array is empty.
    }
}

Upvotes: 0

jl_
jl_

Reputation: 5539

Issue is how we initialize a field.

Quote from MSDN:

Instance fields cannot be used to initialize other instance fields outside a method. If you are trying to initialize a variable outside a method, consider performing the initialization inside the class constructor.

One way to go about with this is:

public class Gen : MonoBehaviour
{
    public Item[] Items { get; set; } = new Item[] { };
    int iNum = -1;

    //Awake is called once by Unity since Gen inherits from  MonoBehaviour
    public void Awake()
    {
        iNum = Items.Length;
    }
}

Upvotes: 2

Theo
Theo

Reputation: 885

The problem is that you are assigning a field from a property of another field. Fields are initialized on construction. To get what you want, the length needs to be accessed from within a property or method like:

public class Item : System.Object
{
    public string name;
    public int Radius = 1;
    public GameObject obj;
}

public class Gen : MonoBehaviour {
    public Item[] Items;
    private int iNum { get { return Items.Length; /*add null check here to be safe*/ } } 
}

Upvotes: 4

FakeCaleb
FakeCaleb

Reputation: 993

You must initialise the array to use this function.

public Item[] Items = new Item[] { *ADD OBJECTS* };

Upvotes: 2

Christos
Christos

Reputation: 53958

The problem is that you declare a field initializer, using another field. You could get instantly that you want, by creating an object and then access get the length of the Items, anytime you want like below:

Items.Length

Upvotes: 3

ps2goat
ps2goat

Reputation: 8485

public Item[] Items; -- Items is null. You are declaring the variable (placeholder), but you are never setting it to a new array.

see https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx

Upvotes: 0

Related Questions