Prefab spawner for looping

I have one prefab that fals down to camera and when his position is hald of camera height it will trigger spawner that will spawn same prefab one and a half height of camera and when he thouces he will triger again and so on…
49289-untitled.png

I have this script attached to prefab but nothing happens

#pragma strict


function Update () {
slab.GetComponent.<Rigidbody2D>().velocity = new Vector2(0,-44);
if (this.transform.position == Screen.height/2){
   spawn_slab ();
}
}
var slab : GameObject;
var spawn_position;

function spawn_slab ()
{
spawn_position = new Vector2(70,Screen.height+(Screen.height/2));
var temp_spawn_slab = Instantiate(slab, spawn_position, Quaternion.identity);
}

There are several reasons why this doesn’t work, but it boils down to the fact that you can’t just “transform.position == Screen.height / 2” and expect it to work.

First and foremost, transform.position is a Vector3 (3 floats), Screen.height is a single float; this by itself does not make any sense. But, if you were to use transform.position.y, there are still two reasons why it won’t work: first of all, transform.position is in world space; the coordinates you see in the Inspector (provided it’s not parented to anything, otherwise those would be the coordinates relative to its parent). Screen.height however is in screen space - it’s the amount of pixels on the screen. The third reason is that you can’t compare two floats to each other using == because of the way floating point numbers are stored in computers; they will never be exactly equal after some operations.

So, what you need to do is first of all check where in screen space the object is, by using camera.WorldToScreenPoint() or camera.WorldToViewportPoint(). Then, using the converted coordinates, you can check if the object is near the screen center, using a predefined offset. For instance:

Vector2 viewportCoords = camera.WorldToViewportPoint(transform.position);
const float margin = 0.05f;
if(Mathf.Abs(viewportCoords.y - 0.5f) < margin)
  spawn_slab();

You should note that depending on the speed of the object, the frame rate and the margin, it is possible that the object is inside this margin for more than one frame, resulting in multiple spawned objects. You will need to put a cooldown on the spawning to prevent this, by remembering the time at which an object was last spawned and only spawning a new object if sufficient time has passed.

A final note: if you keep spawning objects and never removing them, you will eventually run out of memory. For situations like yours, it is recommended to not simply remove an object as it leaves the game area, but instead reuse it instead of deleting and spawning a new object. This can be achieved by simply repositioning the object and resetting its properties as it leaves the game area. This approach handles memory a lot more effeciently - for your use case there won’t be a noticeable performance boost, but it never hurts to practise a best practice.