- Home /
how to check for GameObject is null in array with random
how to check for GameObject is null in array with random ?
unity says : "The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object."
my try with WHILE don't proceed
var ArrayRandomPlace = new Array();
ArrayRandomPlace.Add(empty01);
ArrayRandomPlace.Add(empty02);
ArrayRandomPlace.Add(empty03);
while (ArrayRandomPlace == 0) return ;
Instantiate(card01, ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)].transform.position, transform.rotation); yield WaitForSeconds(0.1);
Destroy(ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)]);
Instantiate(card02, ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)].transform.position, transform.rotation); yield WaitForSeconds(0.1);
Destroy(ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)]);
Instantiate(card03, ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)].transform.position, transform.rotation); yield WaitForSeconds(0.1);
Destroy(ArrayRandomPlace[Random.Range(0,ArrayRandomPlace.length)]);
Edit : here you see my next try, complete code :
// my prefabs cards var card01 : GameObject; var card02 : GameObject; var card03 : GameObject; var card04 : GameObject; var card05 : GameObject; var card06 : GameObject; var card07 : GameObject; var card08 : GameObject; var card09 : GameObject; var card10 : GameObject; var card11 : GameObject; var card12 : GameObject; var card13 : GameObject; var card14 : GameObject; var card15 : GameObject; var card16 : GameObject;
// my empty container positions in scene X, Y, Z var empty01 : GameObject; // 0,0,0 var empty02 : GameObject; // 2,0,0 var empty03 : GameObject; // 4,0,0 var empty04 : GameObject; // 6,0,0 var empty05 : GameObject; // 0,-2,0 var empty06 : GameObject; // 2,-2,0 var empty07 : GameObject; // 4,-2,0 var empty08 : GameObject; // 6,-2,0 var empty09 : GameObject; // 0,-4,0 var empty10 : GameObject; // 2,-4,0 var empty11 : GameObject; // 4,-4,0 var empty12 : GameObject; // 6,-4,0 var empty13 : GameObject; // 0,-6,0 var empty14 : GameObject; // 2,-6,0 var empty15 : GameObject; // 4,-6,0 var empty16 : GameObject; // 6,-6,0
function newField() {
var ArrayRandomPlace = new Array();
ArrayRandomPlace.Add(empty01);
ArrayRandomPlace.Add(empty02);
ArrayRandomPlace.Add(empty03);
ArrayRandomPlace.Add(empty04);
ArrayRandomPlace.Add(empty05);
ArrayRandomPlace.Add(empty06);
ArrayRandomPlace.Add(empty07);
ArrayRandomPlace.Add(empty08);
ArrayRandomPlace.Add(empty09);
ArrayRandomPlace.Add(empty10);
ArrayRandomPlace.Add(empty11);
ArrayRandomPlace.Add(empty12);
ArrayRandomPlace.Add(empty13);
ArrayRandomPlace.Add(empty14);
ArrayRandomPlace.Add(empty15);
ArrayRandomPlace.Add(empty16);
var rndIndex = Random.Range(0, ArrayRandomPlace.length - 1);
var rndPlace = ArrayRandomPlace[rndIndex];
Instantiate(card01, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card02, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card03, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card04, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card05, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card06, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card07, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card08, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card09, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card10, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card11, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card12, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card13, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card14, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card15, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
ArrayRandomPlace.RemoveAt(rndIndex);
Instantiate(card16, rndPlace.transform.position, transform.rotation); yield WaitForSeconds(0.1);
}
here is a screenshot to this : http://i55.tinypic.com/2zhlaja.jpg
Answer by Statement · Mar 03, 2011 at 01:05 PM
while (ArrayRandomPlace == 0) return ;
That is just odd code right there. You're checking if an array is 0? While it is so, it would return?
By the way you need to make the random index one less than length because Random.Range is inclusive. Make it Random.Range(0, ArrayRandomPlace.length - 1)
. The reason you're getting errors is because you're randomly destroying items, and can try do destroy the object twice, or you might destroy an item that you then want to instantiate with the position from the destroyed object. You should remove the object from the array to stop this from happening, or testing if the returned object is null or not.
You should define what you are trying to achieve because that code looks so strange to me.
Basically your code reads to me; "Create a card, at a random location. Then destroy a random location." Did you mean; "Create a card at a random location. Then destroy the card."?
You wrote: "Create a card to random position of empty in scene, then destroy empty, so that the next card do not use the position of last used empty"
So, then we can just remove the object from the list, not remove the object itself :)
Now I think I get it.
// 1. Select a random place. var rndIndex = Random.Range(0, ArrayRandomPlace.length - 1); var rndPlace = ArrayRandomPlace[rndIndex];
// 2. Create card01 at that place. Instantiate(card01, rndPlace.transform.position, transform.rotation);
// 3. Wait 100 milliseconds. yield WaitForSeconds(0.1);
// 4. Remove the place from the list, so it won't show up again. ArrayRandomPlace.RemoveAt(rndIndex);
// 5. (Optionally you might want to destroy the place as well) // Destroy(rndPlace);
If this code is repeated several times, then there won't be any fear of using different places within the call, and same places across different calls because we store the random item so it doesn't change until the next time, and we also remove the object from the list so it's impossible for it to show up again (unless you added it twice etc).
i mean "Create a card to random position of empty in scene, then destroy empty, so that the next card do not use the position of last used empty".
thanks for your suggestions. but i don't know how to realize this
thank you for you help, i edited my first post and see the new full code. now all cards use the same empty position, and this code do not placed all cards, unity says : "ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count."
see also screenshot
Well, you're deleting an item from the array, then trying to delete the same index over and over again. Eventually, there won't be such index (since you're removing items with the same index over and over).