[C#] Making an array of Vector2, but cant access x, or y values...

So I’m creating an array of Vector2 Objects, to store x values, and then i want to read them out, but unity throws this:

what am i missing?

My code looks something like this

public class Main : MonoBehaviour 
{
	public Vector2[] data = new Vector2[]
	{
		new Vector2 (0, 0), 
		new Vector2 (10, 80),
		new Vector2 (20, 110),
		new Vector2 (30, 50)
	};

	private float getvaluey;

	public void Update()
	{
		getvaluey = data.y[2];
	}
}

edit:
stored the values to a temp list and now it works, but is this necessary, I mean like having basically 2 arrays, one massive and another list?

What you actually need is

getvaluey = data[2].y;

You are indexing the array then accessing the Y component of the selected vector.

the problem was, that i couldnt acces it this way for some reason…

indeed for some reason data[2].y is not accessible…
Been fighting with this and only a long workaround as i mentioned works here…

I’ve tried the code example you posted with the change that andeeeee wrote and I have no issues.

using UnityEngine;

public class Main : MonoBehaviour
{
	public Vector2[] data = new Vector2[]
    {
        new Vector2 (0, 0), 
        new Vector2 (10, 80),
        new Vector2 (20, 110),
        new Vector2 (30, 50)
    };

	private float getvaluey;

	public void Update()
	{
		getvaluey = data[2].y;
	}

	private void OnGUI()
	{
		GUILayout.Label( getvaluey.ToString() );
	}
}

The only problem I can think of is that the contents of the data variable has been modified using the inspector. E.g. the array size have been set to something less than 3.

the strangest thing is that indeed at soem point it works, the code itself should be ok, so i’m guessing my Mono has gone nuts somehow… because it provides me only data.y and never data*.y (liek it should be) and some times it works, but mostly it throws me an error.*

That should not be possible.

Have you tried data.y.GetType().FullName to see if it is indeed a float?

restarted the whole thing and now it works, dunno wheres the catch…

hi ravel wud u able to do an arraylist in unity?its like displaying your frends profile pic using plane in unity?sorry didnt know how to use araylist in unity.