Cloning an object seems to cause the original to disappear
I am trying to create the appearance of a never ending field. When an object that is moving gets close to the boundary, I want to create a clone of it and set it at the other end of the field. I do not want to destroy the object until it is out of view range. So, at x>750 I clone the object to x-1500 then, when the player hits 1000, I poof him to -1000 (actually move him back 2000) Finally when an object hits 1500, I destroy the object. Here is the code for the object, which is causing the problem:
if (transform.position.x > 750)
{
if (newx == 0)
{
Instantiate(gameObject, new Vector3(transform.position.x - 1500, transform.position.y, transform.position.z), Quaternion.identity);
newx = 1;
}
}
if (transform.position.x < -750)
{
if (newx == 0)
{
Instantiate(gameObject, new Vector3(transform.position.x + 1500, transform.position.y, transform.position.z), Quaternion.identity);
newx = 1;
}
}
if (transform.position.x > 1500)
{
Destroy(gameObject);
}
if (transform.position.x < -1500)
{
Destroy(gameObject);
}
It appears that everything past 750 is gone. (there should be objects there between 750 and 1500)
Am I missing something?
btw the newx statements are so I only clone that particular object once per boundary cross. I also have the exact same code for y and z axes.
Finally, since I can not tell because objects disappear, does movement (such as the addrelativeforce) transfer to the new clone, or do I have to pass that as well?
Answer by TySim · Jan 23, 2016 at 02:42 PM
Please cancel this thread and close, I found the error of my ways.....
2 problems, the formula was off, and the flag to keep from replicating does not pass like I thought it would.
Your answer
Follow this Question
Related Questions
How to get the position of a new Instantiated prefab 0 Answers
Instantiated object does not inherit rotation. 1 Answer
Cannot set parent for instantiated GameObject. (Exception: Can't destroy Transform component of...) 0 Answers
Vector3.Distance returning offset values due to down force interference. 1 Answer