- Home /
initialising a list with values, and adding a range to a list
So i have a public static generic list of vector2 type, and i'm trying to do this from within a function:
RVPoints.AddRange({
new Vector2(0.0f, 1),//nothing
new Vector2(0.01f, 1),//ULtiny
new Vector2(0.1f, 3),//ULsmall
new Vector2(0.75f, 7),//ULmedium
new Vector2(4.0f, 10),//ULLarge
new Vector2(10.0f, 12),//ULveryLarge
new Vector2(100.0f, 15),//ULMassive
new Vector2(1000.0f, 20),//ULColossal
new Vector2(Mathf.Infinity, 1),
});
This refuses to compile. why?
however, more importantly, this isn't even what i want to do. What i really want is for the list to be initialised with those values to begin with, rather than having to use addrange to put them in, since i'm entering them manually at authortime. i couldn't figure out a way to do that either though
help me out a little here?
Edit to add: The error message i get is "Unexpected symbol {" at the first usage of that character in the example code
If it'll help any, my entire code is here: http://pastebin.com/nPm8Ptkq
FYI, none of this is Unity-related (it's just standard C#) and can easily be googled.
Answer by ahmedbenlakhdhar · Dec 08, 2014 at 04:16 AM
ArrayUtility.AddRange
is a static method, so you should use it like this:
ArrayUtility.AddRange(RVPoints, new Vector2[]{
new Vector2(0.0f, 1),//nothing
new Vector2(0.01f, 1),//ULtiny
new Vector2(0.1f, 3),//ULsmall
new Vector2(0.75f, 7),//ULmedium
new Vector2(4.0f, 10),//ULLarge
new Vector2(10.0f, 12),//ULveryLarge
new Vector2(100.0f, 15),//ULMassive
new Vector2(1000.0f, 20),//ULColossal
new Vector2(Mathf.Infinity, 1),
});
You may declare a public array of Vector2
like this:
public Vector2[] vectors;
Then, in the inspector you find something like this:
At this stage, in the inspector, you will be able to change the size of the array, and fill the appeared fields to populate it with Vector2
elements.
sure it is, but that's not what i'm using, so those instructions are irrelevant
I'm using List.AddRange which is a non-static method of the generic list class. I did mention that this is a generic list, and not an array.
http://msdn.microsoft.com/en-us/library/z883w3dc%28v=vs.110%29.aspx
Check if the second solution (which I added after an edit in the answer above) can fix your problem.
Why are you declaring it as static?
Just write public Vector2[] RVPoints;
and modify it in the inspector.
Answer by Eric5h5 · Dec 08, 2014 at 04:40 AM
You're trying to declare an array like this: { stuff }, which is not something you can do in C#. You're required to specify the type of the array.
As for your other question, you can pass in an array in the list constructor, but that's not really any different from using AddRange. If the number of items in the list doesn't change, then don't use a list, use an array.
check my comment on the original post, i linked my full code in a paste. The list is already declared and a type specified.
passing an array to the constructor sounds about right, can you give me an example of the syntax to accomplish that?
for now i've changed my list to a builtin array, this is probably better. i accomplished what i want by initialising it thusly:
public static Vector2[] RVPoints = new Vector2[] {
new Vector2(0.0f, 1),//nothing
new Vector2(0.01f, 1),//ULtiny
new Vector2(0.1f, 3),//ULsmall
new Vector2(0.75f, 7),//ULmedium
new Vector2(4.0f, 10),//ULLarge
new Vector2(10.0f, 12),//ULveryLarge
new Vector2(100.0f, 15),//UL$$anonymous$$assive
new Vector2(1000.0f, 20),//ULColossal
new Vector2($$anonymous$$athf.Infinity, 1),
};
Still, i'd like to know the syntax to accomplish this with a generic list, for future reference
The list is already declared and a type specified.
That's not what I said. This is what I said:
You're trying to declare an array like this: { stuff }, which is not something you can do in C#. You're required to specify the type of the array.
So, I'm not talking about the list, I'm talking about the array. AddRange takes an array. You can't declare an array by doing { stuff }, you have to declare the type. You might want to brush up on C# syntax. You can look up all this stuff on $$anonymous$$SDN.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to make an IList of a certain type? 2 Answers
Find the highest first value of a vector2 in a list? 2 Answers
How to read X and Y component ( of a vector2) from a list 1 Answer
drag drop a object into list objectes 0 Answers