x


using destroy with an array of GameObjects (C#)

The below code is a snippet of a test of what I'm trying to do. I know it isn't working as intended and I'm getting a "Destroying assets is not permitted to prevent data loss error". What I want to do is to have an array of instantiated GameObjects which I can then manipulate by accessing said array. Anyone have any tips on how I could accomplish this? Thanks in advance.

OLD array declaration.... public GameObject[, ,] gemArray = new GameObject[6, 6, 6];

NEW array declaration.... public Object[, ,] gemArray = new GameObject[6, 6, 6];

     void  Start () {
        for (int i =0; i < 6; i++)
        {
            for (int j =0; j < 6; j++)
            {
                for (int k = 0; k < 6; k++)
                {
                    if ((i != 2 && i != 3) || (j != 2 && j != 3) || (k != 2 && k != 3))
                    {
                        int num = Random.Range(1, 4);


                        switch (num)
                        {

                            case 1:
                                gemArray[i, j, k] = gem1;

                                Instantiate(gemArray[i, j, k, new Vector3(i, j, k), Quaternion.identity);

                                Destroy (gemArray[i,j,k].gameObject);

                                break;

Okay, after some experimentation on my end, I found that if i declare the array as an Object array instead of a GameObject array the operation

gemArray[i, j, k] =  Instantiate(gem1, new Vector3(i, j, k), Quaternion.identity);

works correctly. However, I now cannot access any of the functions that I would have had access to(namely .tag and .transform). I'm not exactly sure of how to go from here...

more ▼

asked Feb 16 '11 at 09:58 PM

digitalConundrum gravatar image

digitalConundrum
156 13 14 21

Do you mean that you want to destroy all gameobjects in the array or the array itself? Have you tried a forloop? :)

Feb 16 '11 at 10:15 PM Kona

My intention is to selective destroy elements in the array, and shift new items into those elements. The algorithm is designed, I just made an assumption as to how this would be handled that turned out to be incorrect.

Feb 16 '11 at 11:15 PM digitalConundrum

An array completely sucks for what you want to do. Use a List.

Feb 16 '11 at 11:31 PM Jessy

"shift" was perhaps an incorrect term to use as I will not actually be doing a linear shift. The order of operations will be: destroy gameobject, raycast to find object that will (physically) shift into that position, shift the transform of the new object, assign the new object to the old object's index, then null the index it originally occupied. I suppose a triply-linked list could do the job, but there would be more operations than I'd like in its execution.

Feb 16 '11 at 11:50 PM digitalConundrum

Don't write "solved" in the title, mark the answer as accepted.

Feb 17 '11 at 12:39 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Problem solved, an explicit casting cleared up all issues, thanks to all who commented.

solution:

declaration...

public GameObject[, ,] gemArray = new GameObject[6, 6, 6];

casting...

gemArray[i, j, k] = (GameObject)Instantiate(gem1, new Vector3(i, j, k), Quaternion.identity);

more ▼

answered Feb 17 '11 at 12:24 AM

digitalConundrum gravatar image

digitalConundrum
156 13 14 21

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2097
x1683
x1363
x764

asked: Feb 16 '11 at 09:58 PM

Seen: 2200 times

Last Updated: Feb 17 '11 at 12:38 AM