1

Hi in my game I have an array of all guns included in the game. I also have an array of the available guns in the game. Currently I am looping through all the guns in the game and if they are unlocked I will add them to the available guns array. For some reason though I cannot figure out how to add a game object to an array.

public Gun[] guns;

public Gun[] availableGuns;

void Start(){

for (int i = 0; i < guns.Length; i++) {
            if ((GameDataManager.publicInstance.gunAvailability & 1 << i) == 1 << i) {
                availableGuns [i] = guns [i];
            }
        }

    }

so essentially I am having problems with this line of code:

  availableGuns [i] = guns [i];

Because It does not work as well as me not wanting the available gun to be in the same position it was in the guns array I just want it to be added onto the available guns array.

Note:

I did edit the guns[] in the inspector whereas I did not change the availableGuns[] at all in the inspector.

5
  • Any reason you can't use generics instead, ie. List<Gun> ? Commented Dec 11, 2016 at 8:41
  • well I would to prefer to keep it an array, but would it be the only way? I say this because I need to call upon this list from other scripts and if I do I will have to use System.Collections.Generic in every one @scotru Commented Dec 11, 2016 at 8:42
  • no.............@Stefan Commented Dec 11, 2016 at 8:46
  • No, but you'll essentially need to recreate the functionality provided by a List or an ArrayList. That is you'll need to keep track of the last element used in the Array. Array's have a fixed length set when they are declared and are not dynamic. So the length will be what you set it to when you create the array. See stackoverflow.com/questions/1056749/… for finding the last used index. You may be able to use some LINQ extension methods here. Commented Dec 11, 2016 at 8:47
  • thanks a lot @scotru Commented Dec 11, 2016 at 8:51

1 Answer 1

3

Use System.Collections.Generic.List<Gun> instead of Gun[]

It gives you the ability to insert at a specific location.

List<Gun> availableGuns;
...
availableGuns = new List<Gun>();
availableGuns.Insert(gun[i], i);

If you want to keep using array:

for (int i = 0; i < guns.Length; i++) {
        if ((GameDataManager.publicInstance.gunAvailability & 1 << i) == 1 << i) {
             //shift from i to the end
             for(int j=guns.Length-2; j>=i; j--)
                  availableGuns[j+1] = availableGuns[j];

             //set the index i
             availableGuns[i] = guns[i];
         }
    }

}

Note that it does not automatically expand the size of the array.

If you want to use a limited number of guns for availableGuns:

int j = 0;
for (int i = 0; i < guns.Length; i++) {
        if ((GameDataManager.publicInstance.gunAvailability & 1 << i) == 1 << i) {
             availableGuns[j++] = guns[i];
         }
    }

}
Sign up to request clarification or add additional context in comments.

10 Comments

would this really be the only way @Bijan as I would prefer to keep using an array?
If you need to extend a collection, then ye List is the only rational way. You could also use an array with extra location. When adding you check whether you have empty slot and if no you would have to create a bigger array, copy all the items and so on. Or you can use a List.
I don't understand the down-vote and this is a valid answer.
@Peyton If availableGuns array is not initialized with the length of Guns it surely throws index out of bounds exception.
wait would all array[ ] be the equivalent of availableGuns [ ] @Bijan
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.