Spawning according to screen width

Im trying to make an array of different cubes spawn according to screen width but for some reason my cubes are spawning ridiculously high off the screen. when i just want them above the camera at all times.

here is my code
using UnityEngine;
using System.Collections;

public class Spawning : MonoBehaviour {

	public GameObject[] blocks;
	public float _delay1 = 1;
	public float _delay2 = 1;

	// Use this for initialization
	void Start () {
		InvokeRepeating ("Spawnme", _delay1, _delay2);


	}
	
	// Update is called once per frame
	void Update () {
	
	}
	void Spawnme(){
		int thingstospawn = Random.Range (0, blocks.Length);
		float screenspawn = Screen.width;
		Vector3 spawnpoint = new Vector3 (0, Random.Range(-1 * screenspawn/2,screenspawn/2), 0);
		Instantiate (blocks [thingstospawn], spawnpoint, Quaternion.identity);
	}
}

thank you in advance.

To me, it sounds like you’re having trouble converting between local and world space. By the way you’re describing it, you will want to use TranformPoint();

Read the documentation to be sure, but here’s my estimation on what you’d want.

 void Example() {
        float constantYPos = 1;
        thePosition = transform.TransformPoint(Random.Range(-1 * Screen.Width/2,Screen.Width/2, constantYPos, 0);
        Instantiate(someObject, thePosition, someObject.transform.rotation) as GameObject;
    }

Screen.width gives you the width of the screen in pixels, you shouldn’t be using it to place objects in the world.

If you have a perspective camera you should check the width and height of the camera frustum at a certain distance (it depends on how close or far you want your objects to spawn). With the width at that distance you can place objects outside the frustum. You can get the size of the frustum doing this: Unity - Manual: The Size of the Frustum at a Given Distance from the Camera

If you have an orthographic camera it doesn’t matter how close or far the objects are from the camera, and you can just get the orthographicSize of the camera object (orthographicSize is half the width and height of the world space that’s displayed on that camera).

Unity uses world space units for 3D object placement, not pixels. Screen.width is the number of pixels wide the screen is, which doesn’t really have anything to do with world space units. You could convert Screen.width to world space using Camera.ScreenToWorldPoint, but really you should pick a specific absolute number and use that. Otherwise the gameplay will change depending on the aspect ratio of the screen, which is almost never what you want.