Spawn Object in the sides of the screen

Hi.
I’m trying to spawn gameobject and the X needs to be a random number BUT between the Screen left side and right side:

Vector3 final = Camera.main.ViewportToWorldPoint (new Vector3 (Random.value, 1, 10));

Instantiate(cube,final,Quaternion.Euler(0, 180, 0));

It works great but what if the game object spawns too left or too right?
In that situation half of the object is visible and the other half you cant see it because its out of the range…

any suggestions?

Use Random.Range and define a “safe zone” between 0 and 1 (viewport coordinates)

float min = 0.2f;
float max = 0.8f;
Vector3 final = Camera.main.ViewportToWorldPoint(new Vector3(Random.Range(min, max), 1, 10));