Skip to content
View in the app

A better way to browse. Learn more.

LCPDFR.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Storing Vector3 Coordinates in an array?

Featured Replies

So, apologies if I post this in the wrong section or anything like that, I'm still kinda new to all this.

 

I am trying to store a few map coordinates in an array in c# and I'm having a bit of trouble figuring out how to store them.

Below is what I tried a second ago, but I've been messing with it for about 15 minutes and im lost. What is the proper way to do this?

 

 Vector3[] hotelLocation = new Vector3[3];
 hotelLocation[1] = (436.8632f, 216.6585f, 104.1104f);

 

Edit:

So I figured out what I was doing wrong and I did fix it. The solution I found is below.

Vector3[] hotelLocation = new Vector3[3];
hotelLocation[1] = new Vector3(436.8632f,216.6585f,104.1104f);

 

It feels super redundant. If there is a better way, someone please let me know.

Edited by MayoInYourMouth

Talking a whole lot of nonsense - since 1999

  • 1 month later...

This is the correct way to do this, however keep in mind that arrays are zero-indexed (meaning the first element is actually element #0). 

 

The reason your original code wasn't working was you created an array that contains Vector3 objects. (See link to learn more about what an object is) When you set the value at index 1 of the array, you didn't create an object, hence why your code worked when you used new Vector3(x, y, z) -- you created an object which could then be held in the array that stores that type of object. 

 

In your case, you have created an array that contains 3 elements. 

To set the first element, you would use:

hotelLocation[0] = new Vector3(436.8632f, 216.6585f, 104.1104f);

To set the second and third elements respectively, you would use:

hotelLocation[1] = new Vector3(componentX, componentY, componentZ);
hotelLocation[2] = new Vector3(componentX, componentY, componentZ);

 

Since you created an array that contains 3 elements, if you were to attempt to set the value of index #3, you would get an IndexOutOfRangeException because the array is zero-indexed and the size is three so the largest index of the array is 2. 

hotelLocation[3] = new Vector3(componentX, componentY, componentZ); // Throws IndexOutOfRangeException

Output: 
Unhandled Exception:
System.IndexOutOfRangeException: Index was outside the bounds of the array.

 

 

Edited by Cavasi
Fixed code

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.