Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 coryl · Jun 30, 2011 at 07:49 AM · arrayvector3arrays

Can't build a Vector3 array

TLDR: Anyone have a working solution for inserting Vector3's into an array?

Hello friends, I'm trying to build a Vector3 array to store a bunch of locations. I started by simply using

var positions = Vector3[]

However, I soon learned that you can't use .Push or .Add with Vector3 for whatever reason. So I searched and found this solution: [http://answers.unity3d.com/questions/60191/arraypush-for-vector3-or-how-to-add-items-to-vecto.html][1]

So I tried using his first sample code:

 import System.Collections.Generic;
  var positions = List.();
  function AddItem (var item : Vector3) {
       positions.Add(item);
  }

However, Unity would give errors regarding an "Unexepected token" for the period on var positions = List.();. I tried to remove the period, and got this error: BCE0157: Generic types without all generic parameters defined cannot be instantiated. Thats about the limit of what I know about List, so I decided to try his next solution.

  var positions : Vector3[];
  function AddItem (var item : Vector3) : Vector3[] {
      var temp : Vector3[] = new Vector3[positions.Length + 1];
      temp[temp.Length - 1] = item;
      for(var i : int = 0; i < positions.Length; i++) {
           temp[i] = positions[i];
      }
  }

There were no errors on this code, however for whatever reason, this doesn't actually insert data into the positions array! I checked that the item parameter is passing through successfully on AddItem(), and it is. So its just not being stored in the array for whatever reason.

If anyone has a working solution to this, would be much appreciated! Thanks [1]: http://answers.unity3d.com/questions/60191/arraypush-for-vector3-or-how-to-add-items-to-vecto.html125

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

2 Replies

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

Answer by Jesus_Freak · Jun 30, 2011 at 08:17 AM

in your first code, just replace positions with:

 import System.Collections.Generic;
 
 var positions = List.<Vector3>();
 function AddItem(var item : Vector3) {
     positions.Add(item);
 }

that way, you can keep you var names. and that will work.

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
avatar image
0

Answer by GuyTidhar · Jun 30, 2011 at 08:16 AM

You can use "Array" (http://unity3d.com/support/documentation/ScriptReference/Array.html)

 var vectorArray : Array = new Array();
 function AddItem(item : Vector3)
 {
   vectorArray.Add(item);
 }
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 coryl · Jun 30, 2011 at 08:47 AM 0
Share

Hi, I think this doesn't work. I'm running another function that will utilize the vectorArray[i] as a parameter. However, the parameter is set to a Vector3 type, and I think this type of array is just an Object type? Unity spits back: "The best overload for the method (..UnityEngine.Vector3)is not compatible with the argument list (..Object). Thanks for your help though!

avatar image Eric5h5 · Jun 30, 2011 at 03:42 PM 0
Share

Array is slow and not statically typed. List is much better.

avatar image GuyTidhar · Jun 30, 2011 at 04:49 PM 0
Share

I never use array. Work in C# anyways.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Im misunderstanding arrays. 1 Answer

Vector3 Array Empties Its Self Immediately After Being Initialized 1 Answer

Array index is out of range 2 Answers

How Do I Add An Instantiated Object To An Array? 3 Answers

How to Clone Array1 into Array2 in Unity? Using JavaScript. 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