• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

My array does not update when object is destroyed. How do I fix it? (java) 2 Answers

if close to one of a kind? 2 Answers

IndexOutOfRange error on iPhone, but not on PC/Player 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges