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 Swindy · Aug 19, 2014 at 12:57 AM · c#arrayvector3

C# - Creating an Array of Vector 3s

I am trying to create 2 Vector 3 arrays, one to hold positions and one to hold rotations. The issue is on the first line of the start function. I get the error...

IndexOutOfRangeException: Array index is out of range. CheckpointPos.Start () (at Assets/Scripts/CheckpointPos.cs:18)

I've looked around the forums to find answers but I can't find anything that works =( I also discovered if I try to edit the size of the array in the inspector, it automatically resets to 0. Any help appreciated and I'll try answer any questions/clarify anything.

 public Vector3[] SpawnPos = new Vector3[8];
 public Vector3[] SpawnRot = new Vector3[8];
                  
              void Start(){
                 SpawnPos [0] = new Vector3 (-11.4f, 5.2f, 10.8f);
                 SpawnRot [0] = new Vector3 (335.8f, 0f, 0);
                 SpawnPos [1] = new Vector3 (-11.4f, 19.4f, 23.4f);
                 SpawnRot [1] = new Vector3 (305.9f, 0f, 0f);
                 SpawnPos [2] = new Vector3 (-11.4f, 25.4f, 40.2f);
                 SpawnRot [2] = new Vector3 (2.2f, 0f, 0f);
                 SpawnPos [3] = new Vector3 (-11.4f, 21.6f, 57.4f);
                 SpawnRot [3] = new Vector3 (32.7f, 0f, 0f);
                 SpawnPos [4] = new Vector3 (-11.4f, 5.3f, 75.9f);
                 SpawnRot [4] = new Vector3 (90f, 0f, 0f);
                 SpawnPos [5] = new Vector3 (-11.4f, -13.0f, 57.2f);
                 SpawnRot [5] = new Vector3 (51.3f, 185.5f, 184.8f);
                 SpawnPos [6] = new Vector3 (-11.4f, -13.0f, 57.2f);
                 SpawnRot [6] = new Vector3 (51.3f, 185.5f, 184.8f);
                 SpawnPos [7] = new Vector3 (-11.4f, -11.1f, 12.3f);
                 SpawnRot [7] = new Vector3 (51.3f, 0f, 0f);
             }



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
0
Best Answer

Answer by robertbu · Aug 19, 2014 at 01:00 AM

The initialization in code of an a public variable is only done at the time the script is attached. After that, the only thing that matters are changes you make in the Inspector. So if you change the size of the array (or just added initialization code for the arrays) after the script was attached, chose changes will not be reflected in the public variables. So you need to take a look at the size of these arrays in the Inspector. You can also use 'Reset' from the gear drop down on the right side of your component in the Inspector to reread the initialization code.

If it works with the rest of your code, a simpler solution would be to change both arrays to be private.

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 Swindy · Aug 19, 2014 at 01:05 AM 0
Share

Thanks! I hit that reset button and everything appeared as it should :)

avatar image
0
Wiki

Answer by DBar · Aug 19, 2014 at 02:22 AM

I know this appears solved but a bit of more clarification doesnt hurts anyone.

If you try to restart the scene it will lose all your variables initialization if it is not used this way:

     //Variable declaration:
     
     public Vector3[] SpawnPos; 
     public Vector3[] SpawnRot;
     private int spawnTotal;
     
     Start(){
     spawnTotal=8; // dynamic
     SpawnPos = new Vector3[spawnTotal];
     SpawnRot = new Vector3[spawnTotal];
     
     SpawnPos [0] = new Vector3 (-11.4f, 5.2f, 10.8f);
                     SpawnRot [0] = new Vector3 (335.8f, 0f, 0);
                     SpawnPos [1] = new Vector3 (-11.4f, 19.4f, 23.4f);
                     SpawnRot [1] = new Vector3 (305.9f, 0f, 0f);
                     SpawnPos [2] = new Vector3 (-11.4f, 25.4f, 40.2f);
                     SpawnRot [2] = new Vector3 (2.2f, 0f, 0f);
                     SpawnPos [3] = new Vector3 (-11.4f, 21.6f, 57.4f);
                     SpawnRot [3] = new Vector3 (32.7f, 0f, 0f);
                     SpawnPos [4] = new Vector3 (-11.4f, 5.3f, 75.9f);
                     SpawnRot [4] = new Vector3 (90f, 0f, 0f);
                     SpawnPos [5] = new Vector3 (-11.4f, -13.0f, 57.2f);
                     SpawnRot [5] = new Vector3 (51.3f, 185.5f, 184.8f);
                     SpawnPos [6] = new Vector3 (-11.4f, -13.0f, 57.2f);
                     SpawnRot [6] = new Vector3 (51.3f, 185.5f, 184.8f);
                     SpawnPos [7] = new Vector3 (-11.4f, -11.1f, 12.3f);
                     SpawnRot [7] = new Vector3 (51.3f, 0f, 0f);
 }
 
 `

About the correct use of methods and variable spelling for better coding:

CAPS in the first letter are for Methods for better coding. So, SpawnPos spelled this way seems like a method to anyone familiar with this. Please use spawnPos for variables. SpawnPos if it is a method. Its hard at first but do it.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I place the matching Vector3 array values of a Vector3 array in a new array ? 1 Answer

How do I find the farthest verts of a mesh relative to its object position and store them in an array 1 Answer

Setting position of a transform? 2 Answers

How to call a function with Vector3[] argument? 3 Answers

How to check if a vector 3 is in a vector 3 array c#. 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