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 Johnny H. · Jun 27, 2013 at 02:34 PM · c#array

Array iteration problem

Hello, I have the following code:

 for(int i = 0; i < slots; i++)
 {
     if(inventory[i] == GameObject.Find("Key"))
     {
         inventory[i] = null;
         //inventory[i - 1] = null;
         openDoor = true;
         break;
     }
 }

This code iterates through an array to check if the player has the key, if the player does have the key, the index changes to a null value and the door is opened. The problem is, if the variable i is at 1 for example, and the player has the key, when inside the if statement the third element (2) is changed, instead of the second one (1).

I don't know if I'm making myself clear, and I apologize for my english. Thanks.

Comment
Add comment · Show 7
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 amphoterik · Jun 27, 2013 at 02:57 PM 0
Share

That shouldn't happen. How are you deter$$anonymous$$ing this is happening? Can you confirm the value of i at runtime?

 print(i);

Something else is at play here.

avatar image Johnny H. · Jun 27, 2013 at 03:20 PM 0
Share

I ran a bunch of tests on gameplay using Debug.Log, and I don't know why, but if I have the key in the index 0, the loop only enters the if statement on index 1, I know it doesn't make any sense, but this is what's happening :(

avatar image MissingSemicolon · Jun 27, 2013 at 03:34 PM 0
Share

Well i assume it enters on index 1 because GameObject.Find will return null if it can't find that gameobject and i'm guessing index 1 is null?

Are you sure GameObject.Find("$$anonymous$$ey") is ever actually finding the object?

Also remember that GameObject.Find will search your hierarchy for that object, if the '$$anonymous$$ey' gets destroyed after being picked up it won't be able to find it. What happens to the $$anonymous$$ey once the player picks it up?

avatar image Johnny H. · Jun 27, 2013 at 03:40 PM 0
Share

As I said before, I ran a bunch of tests, with various GameObjects in the array, but always with the key in it. If the key isn't in the array, the code simply ignores the if statement. When I do have the key in the inventory, the if statement is executed, but only one index later. After picking up the key I disable the mesh renderer of the object.

avatar image Johnny H. · Jun 27, 2013 at 03:53 PM 0
Share

Wow... It worked... Can you explain why that line of code was the error? Thank you very much by the way :)

Show more comments

1 Reply

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

Answer by MissingSemicolon · Jun 27, 2013 at 03:47 PM

Maybe try altering the check so it looks for the name rather than the actual object and see what happens? e.g.

  if(inventory[i].name == "Key")
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

17 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

Related Questions

Unity 3 GD HotShot-Tutorial Problem with an C#-Array 1 Answer

Saving 30 bools in an array possible? 2 Answers

C# Children and SubChildren Null Reference Exception 1 Answer

Inventory system using an array... 3 Answers

Distribute terrain in zones 3 Answers


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