Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
7
Question by maggot · Sep 23, 2012 at 07:29 PM · c#arrayvector3initializedirect

Initializing an array of Vector3 in c sharp #

Currently I'm initializing an array of Vector3 in C# like so :

 Vector3[] positionArray = new Vector3[4];
 positionArray[0] = new Vector3(0.0f,0.0f,0.0f);            
 positionArray[1] = new Vector3(0.1f,0.1f,0.1f);
 positionArray[2] = new Vector3(0.2f,0.3f,0.4f);
 positionArray[3] = new Vector3(0.5f,0.5f,0.5f);
 

I've tried initializing the array using shorter methods, but all have failed. Heres an example of one that I tried :

 Vector3[] positionArray = new Vector3((0f,0f,0f),(1f,1f,1f));

And here is the error that Unity reports back :

A field or property `UnityEngine.Vector3' cannot be initialized with a collection object initializer because type `UnityEngine.Vector3' does not implement `System.Collections.IEnumerable' interface

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
26
Best Answer

Answer by whydoidoit · Sep 23, 2012 at 07:39 PM

The other way to initialize the array is like this:

    Vector3[] positionArray = new [] { new Vector3(0f,0f,0f), new Vector3(1f,1f,1f) };
Comment
Add comment · Show 1 · 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 maggot · Sep 23, 2012 at 08:07 PM 1
Share

It works !

avatar image
7

Answer by Dave-Carlile · Sep 23, 2012 at 09:14 PM

You can also use object initializer syntax:

 Vector3[] positions = { new Vector3 { x = 0, y = 0, z = 0 }, 
                         new Vector3 { x = 1, y = 1, z = 1} };


In this case using the constructor (as Mike did) is more concise.

Comment
Add comment · Show 1 · 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 majmun · May 04, 2020 at 06:16 PM 0
Share

Thank you! You helped me with my a little bit more complex question with custom class array Here it is for others who are seeking same or similar answer:

 public class Sapling
 {
     public saplingSortField[] saplingSort = {   new saplingSortField() {name = "A"},
                                                 new saplingSortField() {name = "B"},
                                                 new saplingSortField() {name = "C"},
                                                 new saplingSortField() {name = "D"},
                                                 new saplingSortField() {name = "E"},
                                                 new saplingSortField() {name = "F"}};
 }
 
 public class saplingSortField
 {
     public string name;
     public int value = 1;
 }

my problem was - I wanted to preset these 6 fields every time "Sapling" is called with no other possible field options. When I call "new Sapling()" I'll get these fields without 'manually' adding them after making new Sapling

Exact problem I had when I was making ingame resources (gold, wood, stone,...) I wanted them to be in same Array but they need name, value, icon,.... also there must not be option for calling "resources" and getting different names, they all must be exactly same in every unit/building/player script which works with resources. I did it in some other way that solved the problem but now I need to remake it in this - correct way.

avatar image
0

Answer by DuckOfDoom · May 13, 2014 at 11:31 AM

Or, if you need fixed array length for copying (like you would use in mesh.vertices) you could do this:

var newVerts = new Vector3[verts.Length - 10];

Comment
Add comment · 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

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

14 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

Related Questions

Why does my program crash when allocate and try to store variable in Vector3 array? 1 Answer

Rotate Vector3 array around a point 1 Answer

Accessing Vector3s of GameObjects in Arrays (JS) 1 Answer

Rotating object towards transform array 2 Answers

Multiple Fire Points 2 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