- Home /
GUI destroy instantiated Objects
Hello,
I've been scouring these forums for hours, and made lots of progress, but this is really stumping me.
Basically i have a GUItexture that i want to destroy a bunch of Cubes, spawned by another GUItexture. I'm a programming noob coming from Maya, i'm getting this error;
Assets/Scripts/KillSpawned.js(21,12): BCE0023: No appropriate version of 'UnityEngine.Object.Destroy' for the argument list '(System.Type)' was found.
from this script;
var spawn : GameObject[];
spawn = GameObject.FindGameObjectsWithTag("Spawned");
var exceptions : GameObject[];
var recoveryTime = 10;
private var delay = 0;
function Update () {
if (delay>0){delay -=1;}
if (delay==0){
if(Input.touchCount == 1){
var currentTouch : Touch = Input.touches[0];
if(currentTouch.phase == TouchPhase.Began && guiTexture.HitTest(currentTouch.position)){
if (gameObject.CompareTag ("Spawned"))
for (var spawined in spawn)
{
Destroy(GameObject);
}
}
}
}
}
...any help would translate directly to extra hair on my head.
Thanks heaps.
Not quite sure I understand this- how does your GUITexture instanitate objects? The GUITexture doesn't do that!
Answer by syclamoth · Oct 24, 2011 at 09:07 AM
Your problem boils down to one letter, actually-
Where you have the line
Destroy(GameObject);
it is trying to destroy the class 'GameObject'- not any particular GameObject, but the entire class. (This makes even less sense than it sounds like).
Instead, you should be using the automatic lookup function to find the attached gameObject-
Destroy(gameObject);
Or, in fact, for that specific case,
Destroy(spawined);
Now, remember that this will only work once, unless you create new objects to fill the gaps which are now left in your array.
There is one other problem, actually- you should be doing the line
spawn = GameObject.FindGameObjectsWithTag("Spawned");
inside of the Start function, not directly under the variable declaration.
Answer by siberman · Oct 24, 2011 at 10:43 AM
Hey Mate, thanks for the reply,
Firstly to explain my scene more clearly, there is a script attached to the GUITexture which instantiates 10 instances of a Cube prefab when touched. This second button is meant to kill them all at once, hopefully...
I've made the suggested amendments, and the error message has left, yea, but i'm still not experiencing the poly death i was hoping for. This is with,
Destroy(gameObject);
when i use,
Destroy(spawn);
i get this error
Assets/Scripts/KillSpawned.js(23,12): BCE0023: No appropriate version of 'UnityEngine.Object.Destroy' for the argument list '(UnityEngine.GameObject[])' was found.
I threw in a quick print "kill" to make sure things are above board on that side, and all seems well.
any further ideas?
Cheers.
Sorry, I made a mistake- I meant
Destroy(spawined);
I forgot which one was the member, and which one was the array!
no need to apologise mate, I myself make mistakes every now and then ;)
Unfortunately although the script seems to be 'working' (no errors) it's still not destroying the cubes, it seems as if the array isn't getting populated or something. Is there a way to check this? call and print array members?
Getting there....
Well, you know that 'for' statement? Try putting a Debug.Log("is it working?"); into it, just before the Destroy line. See if it gets called at all!
Remember that if you use GetComponentsInChildren in Start(), it will only get the objects with that tag in the very first frame. If you want to get everything with that tag in the moment that you need to destroy them, you should put that line just before your for loop, ins$$anonymous$$d!
Sweet! Got it!
I moved FindGameObjectsWithTag to just before the for loop as you suggested, and then i deleted "if (gameObject.CompareTag ("Spawned"))" from the same position, as it seemed redundant... more of an instinct call than anything, now working beautifully, Thanks Heaps.