Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nanako · Dec 08, 2014 at 03:57 AM · listvector2initialisation

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nanako · Dec 08, 2014 at 04:30 AM 0
Share

If it'll help any, my entire code is here: http://pastebin.com/nPm8Ptkq

avatar image Eric5h5 · Dec 08, 2014 at 04:42 AM 0
Share

FYI, none of this is Unity-related (it's just standard C#) and can easily be googled.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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:

alt text

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.


array.png (3.1 kB)
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nanako · Dec 08, 2014 at 04:28 AM 0
Share

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

avatar image ahmedbenlakhdhar · Dec 08, 2014 at 04:42 AM 0
Share

Check if the second solution (which I added after an edit in the answer above) can fix your problem.

avatar image Nanako · Dec 08, 2014 at 04:46 AM 0
Share

can't be done with a static variable, so sadly not.

avatar image ahmedbenlakhdhar · Dec 08, 2014 at 04:49 AM 0
Share

Why are you declaring it as static?

Just write public Vector2[] RVPoints; and modify it in the inspector.

avatar image Nanako · Dec 08, 2014 at 05:53 AM 0
Share

becuase i'm not instancing this class.

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nanako · Dec 08, 2014 at 04:47 AM 0
Share

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?

avatar image Nanako · Dec 08, 2014 at 06:37 AM 0
Share

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

avatar image Eric5h5 · Dec 08, 2014 at 07:09 AM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges