Hi,
If I’m using an array, say:
private var maxNumberOfSeeds : int = 10;
private var SeedsPool : Array = new Array ();
and trying to instantiate it:
private var i : int = 0;
function PD() {
var wPosA = self.position + Vector3(-20,0,0) + Vector3(i * 2.0,0,0);
var wPosB = self.position + Vector3(2,0,0) + Vector3(i * 2.0,0,0);
var wPosC = self.position + Vector3(0,0,2) + Vector3(0,0,i * 2.0);
var wPosD = self.position + Vector3(0,0,-2) + Vector3(0,0,-i * 2.0);
//instantiate pool
if (SeedsPool.length < maxNumberOfSeeds) {
i ++;
SeedsPool.Add(Instantiate(plantSeed, wPosA, Quaternion.identity));
SeedsPool.Add(Instantiate(plantSeed, wPosB, Quaternion.identity));
SeedsPool.Add(Instantiate(plantSeed, wPosC, Quaternion.identity));
SeedsPool.Add(Instantiate(plantSeed, wPosD, Quaternion.identity));
}
//reset instantiation pool
if (i >= 10){
i = 0;
}
//clear instantiation pool
if(SeedsPool.length > maxNumberOfSeeds) {
Destroy(SeedsPool[0]);
SeedsPool.Shift();
}
}
I get the error:
Can’t destroy Transform component. If you want to destroy the game object please call ‘Destroy’ on the game object instead. Destroying the transform component is not allowed.
If, however, I only use one “SeedsPool.Add(Instantiate…)” it’ll work without error.
Any ideas without having to create 4+ arrays?
Cheers.
Your array SeedsPool is apparently an Array of Transforms. To destroy the gameObject of the transform, do this:
Destroy(SeedsPool[0].gameObject );
Which returns the error:
BCE0019: ‘gameObject’ is not a member of ‘Object’.
the problem with JS Array class is that it’s type is defined as Object, which isn’t really helpfull. You actually should avoid using Array() and try to use a built-in array, which is also better for performance. So do this:
private var SeedsPool : Transform[];
Note that this is a static array, so you can’t adjust it’s size by using Add(), I use C# and the System.Collections.Generic.List<> is a fast type defined version of the JavaScript Array() class.
Are you using JS for a very long time or have you just started scripting?
If you just started or don’t mind switching to C#, I would highly recommend it
I suppose I’ll just approach it in another way, or see if Lists will do.
I’ve been using JS for a while, and have even converted some C# references/examples back to JS for my purposes but in no way am I great with C#. I understand that it’s a better language, and it’s purposes in and outside of Unity. Perhaps in the near future I’ll delve into it.
I haven’t done alot with JS, since I’ve heard there were some bad things about it compared to C# (like this) when I dived into programming. The syntax is almost the same as JS and it will give you a better picture of what is actually going on than JS.
D’oh
I can see why the problem occured when I added those other three instantiations, I must’ve also edited the prefab variable -
var plantSeed : Transform;
should be
var plantSeed : GameObject;
Thus fixing the Destroy issue.