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 Theacesofspades · Dec 08, 2014 at 10:06 PM · c#errorarrayexists

how can i check if index exists?

I am trying to check if there is an object to any side of it in the 3d array. My function to check is :

 void UpdateSides ()
     {
         Vector3 myPos = transform.position;
         int x = (int) myPos.x;
         int y = (int) myPos.y;
         int z = (int) myPos.z;
         
         if (levelData[x, y, z] != null)
         {
             if ( x < levelData.GetLength(0) && y < levelData.GetLength(1) && z < levelData.GetLength(2))
             {
                 print ("Pass GetLength Test");
                 if (x-1>-1 && y-1>-1 && z-1>-1)
                 {
                     print ("Pass Over 0");
                     
                     if (levelData[x, y+1, z] == null)
                     {
                         print ("Nothing Above");
                     }
                     if (levelData[x, y+1, z] != null)
                     {
                         print ("Something Above");
                     }
                     if (levelData[x, y-1, z] == null)
                     {
                         print ("Nothing Below");
                     }
                     if (levelData[x, y-1, z] != null)
                     {
                         print ("Something Below");
                     }
                     if (levelData[x+1, y, z] == null)
                     {
                         print ("Nothing Right");
                     }
                     if (levelData[x+1, y, z] != null)
                     {
                         print ("Something Right");
                     }
                     if (levelData[x-1, y, z] == null)
                     {
                         print ("Nothing Left");
                     }
                     if (levelData[x-1, y, z] != null)
                     {
                         print ("Something Left");
                     }
                     if (levelData[x, y, z+1] == null)
                     {
                         print ("Nothing Forward");
                     }
                     if (levelData[x, y, z+1] != null)
                     {
                         print ("Something Forward");
                     }
                     if (levelData[x, y, z-1] == null)
                     {
                         print ("Nothing Back");
                     }
                     if (levelData[x, y, z-1] != null)
                     {
                         print ("Something Back");
                     }
                 }
             }
         }
     }


I have been trying to figure out what i am doing wrong and i just cant seem to find it. Im trying everything. Im using one of the lines that was recommended and now it still isnt working.

Comment
Add comment · Show 1
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 theLucre · Dec 08, 2014 at 10:25 PM 1
Share

Try levelData.GetLength( index ) where the index is the dimension of your array See: http://stackoverflow.com/questions/2044591/what-is-the-difference-between-array-getlength-and-array-length

2 Replies

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

Answer by Theacesofspades · Dec 09, 2014 at 11:01 PM

Thank you ahmedbenlakhdhar. I used your code. The only thing i had to fix was that i put a -1 at the end to get one shorter of the length. The code i used is:

 void UpdateSides ()
     {
         Vector3 myPos = transform.position;
         int x = (int) myPos.x;
         int y = (int) myPos.y;
         int z = (int) myPos.z;
         
         if (levelData[x, y, z] != null)
         {
             if (x < levelData.GetLength(0)-1)
             {
                 if (x-1>-1)
                 {
                     if (levelData[x+1, y, z] == null)
                     {
                         print ("Nothing Right");
                     }
                     if (levelData[x+1, y, z] != null)
                     {
                         print ("Something Right");
                     }
                     if (levelData[x-1, y, z] == null)
                     {
                         print ("Nothing Left");
                     }
                     if (levelData[x-1, y, z] != null)
                     {
                         print ("Something Left");
                     }
                 }
             }
             if (y < levelData.GetLength(1)-1)
             {
                 if (y-1>-1)
                 {
                     if (levelData[x, y+1, z] == null)
                     {
                         print ("Nothing Above");
                     }
                     if (levelData[x, y+1, z] != null)
                     {
                         print ("Something Above");
                     }
                     if (levelData[x, y-1, z] == null)
                     {
                         print ("Nothing Below");
                     }
                     if (levelData[x, y-1, z] != null)
                     {
                         print ("Something Below");
                     }
                 }
             }
             if (z < levelData.GetLength(2)-1)
             {
                 if (z-1>-1)
                 {
                     if (levelData[x, y, z+1] == null)
                     {
                         print ("Nothing Forward");
                     }
                     if (levelData[x, y, z+1] != null)
                     {
                         print ("Something Forward");
                     }
                     if (levelData[x, y, z-1] == null)
                     {
                         print ("Nothing Back");
                     }
                     if (levelData[x, y, z-1] != null)
                     {
                         print ("Something Back");
                     }
                 }
             }
         }
     }
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
1

Answer by ahmedbenlakhdhar · Dec 08, 2014 at 10:31 PM

Try this:

 if ( x < levelData.GetLength(0)
   && y < levelData.GetLength(1)
   && z < levelData.GetLength(2))
 {
     //your code
 }

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 Theacesofspades · Dec 09, 2014 at 10:33 PM 0
Share

im still getting an error saying the index is out of range at this line:

 if (levelData[x, y-1, z] == null)
avatar image ahmedbenlakhdhar · Dec 09, 2014 at 10:40 PM 0
Share

Did you use GetLength in all of the conditions ?

avatar image Theacesofspades · Dec 09, 2014 at 10:45 PM 0
Share

I put in exactly what you have. Ill edit my question

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

27 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

Related Questions

Multiple Cars not working 1 Answer

C# array not behaving as expected 0 Answers

IndexOutOfRangeException help on debug 3 Answers

Cannot convert float to int when only using floats (C#) 3 Answers

How can I make this GameObjects array remain permanent when i activate? 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