|
Hi, i'm new to the forums and Unity/JS. I've been banging my head on this for 2 days now. i've tried so many different solutions and have looked at every relevant post online. The worst part is that i swear there is a very simple solution to this. I've got a grid of instantiated objects (currently all from a single prefab) which i'm adding to an array by mouse clicking the object. Once i reach the desired selection count I'd like to Destroy those instances and remove them from the array. Here's the relevant script: The above script is on an empty object in my scene. On my prefab object there is the "CleanUp" script which looks like this: Any help would be greatly appreciated. If you can spell it out, so i can learn from it/my mistakes, that would be even better! Thanks.
(comments are locked)
|
|
I think your problem is doing the RemoveAt(i) from inside the loop. RemoveAt will remove the item from the array and shift everything after it down by one. This means that what was at index 4 now is at index 3. Well, if i was 3, then at the end of the loop it increments to 4, the value at 4 is now the value that was at 5 on the last iteration. Effectively you will be removing every other item until you reach the half-way point, and then you will be indexing off the end of the array (which causes an error). The fix is to just wait until after the loop and do 'selected.Clear()' Wicked that worked! Thank you!
May 26 '11 at 06:46 AM
Drexster
(comments are locked)
|
|
function Update () { for(var i : int = 0 ; i < 5 ; i++) { var position : Vector3 = Vector3(Random.Range(-10.0,10.0),Random.Range(-10.0,10.0),40.0); go[i] = Instantiate(prefab,position,Quaternion.identity); } for(var j : int = 0 ; j < 5 ; j++) { Destroy(go[j]); } whats wrong with this code? Please help me out ..novice.
(comments are locked)
|
|
I've gotten a bit closer after a break, some food and fresh air. I now have 2/3 of my objects destroying, only the first and third objects from the array are destroying. I'm assuming it's something to do with one of my for loops? here are the iterated scripts: and the CleanUp script: Getting closer. Any thoughts/comments?
(comments are locked)
|
