Random position for different objects

hey all! I’m totally noob with coding. I was wondering if someone can help me out with this. First of all is important we are talking about 2D game. I have 3 different Images and I want at the begin of the level this 3 Images to be loaded randomly in the scene without overlapping themselves. Any ideas? Thx in advance!

You mean just placed anywhere in 2D space as long as they aren’t overlapping each other?

I’d do it using sprites, you can just drag some images into Unity, set them to Sprite (2D) mode, and then take several routes to displaying them. In the editor, create a few new GameObjects in the scene (locations unimportant), throw a Sprite Renderer component on them, and then load the “sprites” you just made into the Sprite member of the Sprite Renderer component. Resize it manually on the screen so it’s the right size you think you’ll need, then drag the whole GameObject down to the assets to make a Prefab from it. Now you can either use the script to instantiate the prefabs, just changing the Transform position to random spots, or you can leave the GameObjects up in the scene and move them around / disable/enable them when needed and never had to worry about even messing with prefabs.

I’ll post some code as soon as I get home to my real computer, this laptop is a bit annoying.

UPDATE:
I was going through the math, which is surprisingly annoying for comparing rects, when I realized there’s something that already does this. I’m not going to post the code for you since I’m still on my laptop and can’t test it though.

Depending on whether you’ve chosen the prefab or leave-in-the-scene route, both of which I mentioned in the last comment, you may have to instantiate your GameObjects into the scene or just locate them by tag or something and load them into variables so you can play around with them.

Regardless of whether you have prefabs or existing objects though, you should have already set them with Sprite Renderers and set the Sprite target to the sprites you want, so all you have to do in the code is use the random number range generator, as seen here, to set the X/Y coordinates for the first GameObject (image #1), then grab the Sprite Renderer’s Bounds using GetComponent on it and assign it to some variable. Move the second GameObject (image #2) using the same random-generation rules, grab the Sprite Renderer’s Bounds for that one too, and use Bounds.Intersect(otherBounds) to find out if they’re overlapping. If they are, then move the second GameObject again until they aren’t overlapping anymore. Do the same thing for the third GameObject (image #3), comparing to both the first and second before finally leaving it alone.

Doing all of this entirely by code would be quite annoying, so I highly recommend using the prefab or left-in-scene-but not-always-active methods.

UPDATE #2:
If using prefabs, the following should work I think. If not, it’ll at least be a good start for you. Pay special attention to the size of the sprites. You’ll need to adjust the Random.Range values in the PlaceRandomly function so that the min and max make sense for your setup, otherwise you may end up in a situation where the first sprite is placed in such a way that the second and third CAN’T be placed without overlapping, and it’ll infinite loop. You can turn the while loops into for loops to set a maximum number of attempts pretty easily, but I wanted to make it obvious that an infinite-loops situation can occur here. If the game REQUIRES that three sprites be placed, then this kind of obvious limitation makes more sense- so it’ll definitely fail if a problem occurs.

using UnityEngine;

public class TestScript : MonoBehaviour {
    public GameObject spritePrefab1;
    public GameObject spritePrefab2;
    public GameObject spritePrefab3;

    void Start(){
        GameObject newSprite1 = Instantiate(spritePrefab1) as GameObject;
        GameObject newSprite2 = Instantiate(spritePrefab2) as GameObject;
        GameObject newSprite3 = Instantiate(spritePrefab3) as GameObject;
        PlaceRandomly(newSprite1);
        Bounds spriteBounds1 = newSprite1.GetComponent<SpriteRenderer>().bounds;
        Bounds spriteBounds2 = newSprite2.GetComponent<SpriteRenderer>().bounds;
        Bounds spriteBounds3 = newSprite3.GetComponent<SpriteRenderer>().bounds;
        while(true){
            PlaceRandomly(newSprite2);
            if(!spriteBounds1.Intersects(spriteBounds2)){
                break;
            }
        }
        while(true){
            PlaceRandomly(newSprite3);
            if(!spriteBounds1.Intersects(spriteBounds3) && !spriteBounds2.Intersects(spriteBounds3)){
                break;
            }
        }
    }

    private void PlaceRandomly(GameObject sprite){
        float tempX = Random.Range(0f, 100f);
        float tempY = Random.Range(0f, 100f);
        sprite.gameObject.transform.position = new Vector3(tempX, tempY);
    }
}

I’m not sure if it’s more polite to edit/update the responses here as I did, or to double-post instead in order to make sure a notification occurs. I’m doing both to be safe (and because I’m tired).