- Home /
Update instantiates more than 1 object
If the object has reach certain position.x, I want that that object clone itself, but in another position. I wrote this code in the Update function:
if(transform.localPosition.x <= -6.26f && canBuild == true)
{
canBuild = false;
Instantiate(gameObject, new Vector3(transform.position.x + 15.65f, transform.position.y, transform.position.z), Quaternion.identity);
}
I use localPosition because the object, in Start() is parent of the camera.
The code seems to run well, but sometimes, it enters twice in the IF sentence, so 2 objects are created instead of one.
Why is happening this? How can I fix it?
Hard to tell just from that. But a few questions spring to $$anonymous$$d...
Does anything ever set canBuild to true? If not then it seems unlikely that the if clause is being entered twice on the same object. By the way, it's not static is it?
How many objects have this component?
And is it possible that the clone is cloning itself?
Logging, including a unique identifier for the cloning object as well as its position, would probably clear it up.
canBuild is initialized as true. It enters in the IF so is not the problem!
Just one kind of object. The floor object. That clones itself when the camera moves right.
Yes, the clone clones itself when it reach the position.x desired.
So how do you know that the second object isn't a clone of the clone?
Try na$$anonymous$$g them with a counter will sort things out
Could just use the instance id. Something like this...
GameObject clone = Instantiate(blah blah) as GameObject;
Debug.Log(this.GetInstanceID()+" just cloned "+clone.GetInstanceID());
Then you'll know if the same object is cloning multiple times, if it's a chain with the first clone cloning another, or something else.
Answer by Nymisu · Feb 20, 2015 at 09:53 AM
Depends on how many you want to spawn. If you want to control exact amounts, you could make either a list or a singular gameobject, as follows:
list<GameObject> horribleMonsters = new list<GameObject>();
//or
GameObject terribleBeast;
if(transform.localPosition.x <= -6.26f && canBuild == true)
{
canBuild = false;
if(terribleBeast == null)
{
terribleBeast = Instantiate(gameObject, new Vector3(transform.position.x + 15.65f, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
}
//or
if(!horribleMonsters.Exists(s => s.identifyingcharacteristicsuchasname("Steve"))
{
GameObject x = Instantiate(gameObject, new Vector3(transform.position.x + 15.65f, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
horribleMonsters.Add(x);
}
}
Keep in mind updateloop can fire multiple times a frame if your game hitches. Try setting timescale to 0 for instance, lots and lots of updateloops within singular frame.
Answer by DarkSlash · Mar 06, 2015 at 02:46 PM
Couldn't find the reason, but instead of Instantiate/Destroy I made an Object Pool and solve this issue and also increased the performance!
Your answer
Follow this Question
Related Questions
[2D] Get the position of an object outside the scope 2 Answers
Does it help performance to first check if some transform attribute has changed before setting it? 1 Answer
moving objects with transform position 2 Answers
Why can't I assign transform.position to a Vector3 object? 3 Answers
Why is rotation offset 1 Answer