- Home /
Copy Array to a Stack
Hi,
Is there a simple way to copy an array to a stack at Start, so when I pop one from the stack I do not end up with a (Clone)
I know "CopyTo" Copies the Stack to an existing one-dimensional Array and "ToArray" Copies the Stack to a new array.
But I want to the the other way, in .js
Thanks.
Can't you just pass the array to the Stack constructor? Or are you trying to merge in the array?
I thought this would do it
#pragma strict
import System.Collections.Generic;
var objectStack : Stack.<Transform> = new Stack.<Transform>();
var objectArray : GameObject[];
private var poolSize : int;
function Awake(){
poolSize = objectArray.length;
_loadPool();
}
function _loadPool(){
for (var c : int = 0; c < poolSize; ++c){
objectStack.Push(objectArray[c].transform);
objectArray[c].SetActive(false);
}
}
But when I pop one out it does not show.
You code looks fine. What happens when you put some Debug.Log() statements in the code? Is the size of objectStack the same as objectArray at the end? If you do a Peek() the stack each time through the loop, does the values in objectArray[c] and the value on the top of the stack match? If you dump the positions of both inside _loadPool after the for() loop, what do you see?
robertbu, Sorry for wasting your time, I've been looking in the wrong place, the problem is in a script on the GameObject, that was stopping the GameObject showing when I'd popped it out of the stack …. O well you live and learn … Thanks for the input, appreciated.
Your answer
