- Home /
script is instantiating many copies instead of one
Hey everyone! I'm still learning so please forgive any stupid mistakes.
I have a script here that spawns a prefab "ActivePiece" that translates in direction. This prefab is a GO with a handful of child objects called "unitActive"- these child objects have "collisionScript" attached to them to detect collision.
When the collision is detected what is SUPPOSED to happen is that the children in the prefab ActivePiece are supposed to unparent, have their position rounded to the nearest integers, then a new object "unitIdle" is instantiated at the position of each of these children. Once the new objects have been instantiated the prefab is destroyed and a new one is spawned, restarting the cycle.
I am running into 2 problems.
Problem A- when collision is detected the new unitIdle objects seem to instantiate at the location (rounded to the integer) of the parent in the prefab rather than at each childs location, from what I can tell.
Problem B- after the first prefab is destroyed and its children have been replaced by the new unitIdle objects (although improperly), rather than spawning a single new prefab it continuously spawns new prefabs AND it also continues to spawn unitIdles, but now at the location where the prefab spawns.
I can't figure this one out with google, so I'm hoping you smart folks can shed some light on this.
#pragma strict
var activePce : GameObject[];
internal var destThis : GameObject;
internal var dest : boolean = false;
var idleUnit : GameObject;
// function to spawn a new game piece
function spawn ()
{
var clone : GameObject;
clone = Instantiate(activePce[(Random.Range(0, activePce.Length-1))], transform.position, transform.rotation);
clone.name = "ActivePiece";
destThis = clone;
}
// spawn initial piece
spawn();
function Update ()
{
//check for active piece and spawn one if one is not active
var unitList : GameObject[];
if (dest == true)
{
dest = false;
spawn();
}
//detect collision
unitList = GameObject.FindGameObjectsWithTag("ActiveUnit");
for (var obj : GameObject in unitList)
{
var cs : collisionScript = obj.GetComponent(collisionScript);
if (cs)
{
if (cs.hit)
{
dest = true;
}
}
}
//if collision is detected- destroy active piece and replace it with idle units
if (dest)
{
for (var obj : GameObject in unitList)
{
var swapUnit : GameObject;
var pos : Vector3;
obj.transform.parent = null;
pos = obj.transform.position;
pos = new Vector3(Mathf.Round(pos.x), Mathf.Round(pos.y), Mathf.Round(pos.z));
obj.transform.position = pos;
swapUnit = Instantiate(idleUnit, transform.position, obj.transform.rotation);
}
Destroy (destThis);
}
}
Don't do this, it doesn't work right:
(Random.Range(0, activePce.Length-1))
See the docs for Random.Range (the integer version). Also, using Update seems like a bad idea. There's no reason to constantly poll for collision when Unity has an OnCollisionEnter callback. Doing FindGameObjectsWithTag every frame is not great for performance either.
You can convert this to a comment. click on more, then convert to comment =]
I figured writing this here would be easier than in a comment...
Thanks for the response Eric. There are reasons that I am using raycasting and not actual colliders to detect my collision, but I did take your advice and re-worked the code a bit to increase efficiency. It also seems to have fixed some of the problems.
The script no longer causes things to be instantiated continuously. However- each prefab should spawn 4 "idleUnits" upon collision at the location that its children existed just before its destroyed. However, upon collision it sometimes spawns the proper 4 "idleUnits" BUT sometimes it spawns 8. Also, all of the "idleUnits" that are instantiated are being instantiated at (0,0,0) rather than the position of their respective child piece. Does this make sense?
Here are the current scripts:
collisionScript
#pragma strict
function Update ()
{
var dir = Vector3.forward;
if (Physics.Raycast(transform.position, dir, .75))
{
var spawner : GameObject;
spawner = GameObject.Find("spawnerNull");
Debug.Log("hit!!!!!");
spawner.GetComponent(spawnScript).newPiece();
Destroy(gameObject);
}
}
spawnScript
#pragma strict
var activePce : GameObject[];
internal var destThis : GameObject;
var idleUnit : GameObject;
// function to spawn a new game piece
function spawn ()
{
var clone : GameObject;
clone = Instantiate(activePce[(Random.Range(0, activePce.Length))], transform.position, transform.rotation);
clone.name = "ActivePiece";
destThis = clone;
}
// spawn initial piece
spawn();
function newPiece ()
{
//check for active piece and spawn one if one is not active
var unitList : GameObject[];
//populate array
unitList = GameObject.FindGameObjectsWithTag("ActiveUnit");
//replace active units with idle units
for (var obj : GameObject in unitList)
{
var swapUnit : GameObject;
var pos : Vector3;
pos = obj.transform.position;
Debug.Log(pos);
pos = Vector3($$anonymous$$athf.Round(pos.x), $$anonymous$$athf.Round(pos.y), $$anonymous$$athf.Round(pos.z));
obj.transform.position = pos;
swapUnit = Instantiate(idleUnit, transform.position, obj.transform.rotation);
Destroy(obj);
}
Destroy (destThis);
spawn();
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Creating an object and setting fields before object begins running 1 Answer
Instantantiate relative to parents position 1 Answer
AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers
How to call a MainClass Funktion ? 1 Answer