• 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 Akisato · Feb 01, 2015 at 05:25 PM · arraynullindexarray of gameobjects

Null first array index and shift rest by 1?

Hello!

I am having an issue shifting all array members down by 1 while deleting the first.

Currenly I have the following code: I set the player's deck and populate it using Unity's inspector. It is set to 30. At this point the entire deck is populated.

I then set the player's hand and set it to 5 in Unity's inspector. The array however isn't populated and everything is left null.

Next I check if the first index in the player's hand is empty, if it is, I copy the first card in the deck to the player's hand. I then call the ShiftDeck function to move everything down by 1.

ShiftDeck works by setting the current index gameobject to the next indexed gameobject. So i = i + 1. This continues until all objects are pushed over, then the last one is set to null.

 public GameObject[] playerDeck;
 public GameObject[] playerHand;
 
 if (playerHand[0] == null)
 {
     playerHand[0] = playerDeck[0];
     shiftDeck(playerDeck);
 }

 if (playerHand[1] == null)
     {
         playerHand[1] = playerDeck[0];
         shiftDeck(playerDeck);
     }
 
 This continues until playerHand[4]
 
 public void shiftDeck(GameObject[] deck)
 {
     for (int i = 0; i < deck.Length - 1; i++)
     {
         if (deck[i] != null)
         {
              deck[i] = deck[i + 1];
         }
     }

              deck[deck.Length - 1] = null;
 }


This however does not seem to work...sometimes other elements in the middle of the array are null rather than the last 5 on the list. Other times it functions just fine. when it does function fine however, player's hand is not copied correctly and all values are left as null.

Anyone have any examples as to how I would properly shift all array indexed items down by 1?

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 Glurth · Feb 01, 2015 at 10:41 PM 0
Share

Other times it functions just fine.

The loop looks O$$anonymous$$ to me. I'm not sure why you have the if function inside your loop. Let it just move any nulls it finds.

 for (int i = 0; i < deck.Length - 1; i++)
     deck[i] = deck[i + 1];
 deck[i] = null;



That being said, you might want to try using a dynamic container, like a List< GameObject > , rather than a static length array. This link looks like a good tutorial on em: http://csharp.net-informations.com/collection/list.htm

avatar image Eric5h5 · Feb 01, 2015 at 10:48 PM 1
Share

Why not just use a List ins$$anonymous$$d of re-inventing the wheel?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Alanisaac · Feb 01, 2015 at 11:35 PM

In situations like this, I would personally use a different collection type. It depends on your particular mechanics, but you might want to consider using:

  • A Queue, if the only behaviors you need are removing from the top and adding to the end of the deck.

  • A List if removing and inserting at certain places in the middle of the deck is also important.

You should still be able to use Unity's inspector to populate the other collection types in the same way. Here's a List example (and don't forget to make sure your code file is using System.Collections.Generic!):

 public List<GameObject> playerDeck;
     public GameObject[] playerHand;
 
     public void MyMethod() 
     {
         if (playerHand[0] == null)
         {
             playerHand[0] = playerDeck[0];
 
             // Remove the first value in the deck
             playerDeck.RemoveAt(0); 
 
             // If you want to preserve the "array shifting" effect, 
             // so your last item is null, just uncomment this next line:
             // playerDeck.Add(null);
         }
 
         // Repeat as needed...
         if (playerHand[1] == null)
         {
             playerHand[1] = playerDeck[0];
             playerDeck.RemoveAt(0);
         }
     }

As to your original question, I'm not sure I can see where the problem is. I will say if in any of those "etc." statements you didn't list, you're accidentally referencing the wrong array, you might end up with null values in odd places. I would encapsulate a method like below, to avoid potential errors in writing that code five times.

 private void DrawCard(int handPosition)
     {
         if (playerHand[handPosition] == null)
         {
             playerHand[handPosition] = playerDeck[0];
             shiftDeck(playerDeck);
         }
     }

Otherwise, see this link. The approach described looks the same as yours.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Array error - Index is less than 0... 3 Answers

Get all GameObjects by variable value 2 Answers

Array index is out of range 1 Answer

How to set a specific gameobject in an array false when it collides with a trigger. 1 Answer

Could you help me with Array.RemoveAt()? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges