- Home /
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...
Do you mean that you want to destroy all gameobjects in the array or the array itself? Have you tried a forloop? :)
$$anonymous$$y 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.
An array completely sucks for what you want to do. Use a List.
"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.
Don't write "solved" in the title, mark the answer as accepted.
Answer by digitalConundrum · Feb 17, 2011 at 12:24 AM
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);
Your answer
Follow this Question
Related Questions
Referencing an instantiated object with a global variable 0 Answers
Destroying 2D Array of Instantated Objects 2 Answers
How can I destroy objects and instantiates new in Array? 2 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Instantiate from array into array? 2 Answers