- Home /
Instantiate and Destroy an object while crossing a line with its clone also
Hello!!
I am new to Unity. I just wanted to know that how I will instantiate an object
when it cross a line which is made up of cube and it happens again and again means the
clone(instantiated object) also creates an object after crossing that line
and the instantiated object destroys when it crosses another line made below in gui
I have attached an image for better understanding
Answer by Kiwasi · Jun 15, 2014 at 10:36 PM
This is a pretty common scenario used in 2D scrollers
In essence you use OnTriggerEnter (or OnTriggerEnter2D). Put a trigger at the top and bottom of your cube. Use instantiate(gameObject)
in the first. Use destroy(GameObject)
in the second.
Destroy and instantiate can be expensive. In some cases it is more efficient to use an object pool. Google it.
or watch in the live training session in the learn section of this sites, object pooling