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 james2238 · Oct 16, 2016 at 05:43 PM · vector22d array

Returning Multiple Variables From a Function

I'm new to unity and trying to use a Vector2 as an index position for a 2D array. I've done a lot of searching but haven't found a solution yet. I tried something like this

 int[,] intArray;
 Vector2 pos = new Vector2(7, 6);
     
 intArray[vectToInt(pos)] = 3;
     
 int vectToInt(Vector2 i) {
     dualInt j;
     j.x = Mathf.RoundToInt (i.x);
     j.y = Mathf.RoundToInt (i.y);
     return j;
 }
     
 public struct dualInt {
     public int x;
     public int y;
 }

to return two integers, but that didn't work. How else could I do it in an efficient way, I was using

 intArray[Mathf.RoundToInt (vector2.x), Mathf.RoundToInt (vector2.y)] = int;

but I want something simpler.

Comment
Add comment · Show 2
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 Sergio7888 · Oct 16, 2016 at 06:16 PM 0
Share

$$anonymous$$any things:
You cant set array elements in C# outside a method, you can only initialize the entire array.
A bidimensional need 2 values coma separated not a struct.< br> You need initialize the array before use it.

avatar image oswin_c · Oct 17, 2016 at 02:36 AM 0
Share

You aren't quite there. You need the comma separated value, and you returned int ins$$anonymous$$d of dual int. Fixing your method:

  int[,] intArray;
  Vector2 pos = new Vector2(7, 6);
      
  intArray[vectToInt(pos).x, vectToInt(pos).y] = 3;
      
  dualint vectToInt(Vector2 i) {
      dualInt j;
      j.x = $$anonymous$$athf.RoundToInt (i.x);
      j.y = $$anonymous$$athf.RoundToInt (i.y);
      return j;
  }
      
  public struct dualInt {
      public int x;
      public int y;
  }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ElijahShadbolt · Oct 17, 2016 at 04:24 AM

I created a struct similar to dualint in james2238's more recent post, which I named int2. It has extension methods for 2D arrays to use the int2 as an index, as well as easy conversion from Vector2's.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Vector2 as 2D Array Index 1 Answer

Deriving and angle from two points 1 Answer

Need help with Vector3s and Vector2s 2 Answers

Overload Vector3's operator + 2 Answers

Joystick to turn object 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