• 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 Fritsl · Oct 08, 2014 at 08:20 PM · c#array

Assigning Vector3 Array values in C#

Yes, this has been debated a lot, yet I'm unable to find a stup similar to mine, nor any help in the Unity documentation, because it's pretty outdated on the C# Array stuff. I have filed this to Unity. In the meantime, I'm STUCK, oh my GOD I hate it, any pointer would be very appreciated, thanks :)

 using UnityEngine;
 using System.Collections;
 
 public class Star_Handler : MonoBehaviour
 {    
         public GameObject[] stars;
         public Vector3[] starMov;
 
         void Start ()
         {
                 stars = GameObject.FindGameObjectsWithTag ("Star");
                 for (int i = 0; i<stars.Length; i++) {
                         starMov [i] = new Vector3 (1.2f, 1.2f, 1.2f);//this line is giving me an Array out of range - why?
                 }
         }
 }
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

1 Reply

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

Answer by Habitablaba · Oct 08, 2014 at 08:39 PM

You aren't initializing starMov to anything, so starMov[i], where i is anything will return array out of bounds.
Try this:

 stars = GameObject.FindGameObjectsWithTag("Star");
 starMov = new Vector3[stars.Length];
 for(int i = 0; i < stars.Length; ++i){
   starMov[i] = new Vector3(1.2f, 1.2f, 1.2f);
 }

Edit: I was overzealous with my parens, fixed now.

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 Fritsl · Oct 08, 2014 at 08:46 PM 0
Share

You are right - I should need to initialize it - I had tried that without success - and unfortunately your code does not do the trick ;/

Something is wrong with

star$$anonymous$$ov = new Vector3[stars.Length]();

it returns

Expression denotes a value', where a method group' was expected

and even if I replace it with say

star$$anonymous$$ov = new Vector3[500);

I still get the OutOfRange error!

avatar image Habitablaba · Oct 08, 2014 at 08:50 PM 1
Share

the code you've posted in your comment makes no sense... is it copy/paste from your actual code, or are you retyping it in here?

If that is the actual code you are using, you are messing up your braces.
Look carefully at my code and your code. See how I use the square brackets around star.Length in the array initializer? See how the code you've posted does not?

avatar image Fritsl · Oct 08, 2014 at 08:56 PM 0
Share

O$$anonymous$$G; you are right, I'm sorry, I was misreading you and basically going totally bra[e blind after pounding that same thing too long :) Thank you very much!!

avatar image Fritsl · Oct 08, 2014 at 08:58 PM 0
Share

"Edit: I was overzealous with my parens, fixed now." Lol - I thought I was going crazy :D Thanks a bunch!!

avatar image Habitablaba · Oct 08, 2014 at 08:59 PM 0
Share

If its all working, don't forget to mark the answer ;)

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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Set default length for an array of elements of a custom class in inspector 0 Answers

How to check if a value exists in an array (C#) 3 Answers

Move files from folder a to folder b 1 Answer


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