Random spawn help

Hi guys, I want to make when I load level that all my sprites randomly spawn.
I tried this but it just clones sprite and I have to make empty object for each sprite and change Vector3.

#pragma strict

var cube : GameObject;
var spawn_position;
var timer = 0.0;

function spawn_cube () {

spawn_position = Vector3(0,0,0);
var temp_spawn_cube = Instantiate(cube, spawn_position, Quaternion.identity);
}

function Start () {

}

function Update () {
timer += Time.deltaTime;
if(timer>0.2)
{
spawn_cube();
timer = 0.0;
}
}

Can someone help me?

Could it be because spawn_position is the same for every Instantiate() call? They all spawn in the same location.

1 Like

So you are spawning the one object (cube) into a collection of different places? If that’s the case create an array or list of empty game objects that are each placed where you want to spawn a cube. Loop through the array/list & instantiate on each one.
If you only want to instantiate in some of the spots then you will need to have a bool for each position & when an object is instantiated the bool is set. The loop will need to choose a random number for the spawn position (# relates to the list/array position), check that the chosen position doesn’t have the bool set (if it does choose another number), & instantiate the cube.

1 Like

I want to load level with sprites on different places so when I press on one sprite another level would load.
I know how to load level by pressing on sprite but don’t know this above said.

Thank you both for helping :smile:

If you are loading whole new levels just have them already set with everything in place, don’t try to instantiate it all when the scene loads

1 Like

Alright, I’ll have to make more than 1000 levels then haha, thank you for helping! :smile:

If the levels are basically the same but the cubes are In Different places then don’t load a new level, just reset the one you are on, move the player to the start spot, destroy the cubes that are there then randomly spawn the new ones.

Example - press here
I have something like this, so when I tap on one sprite it loads another level but with different locations of sprites (android game).
I hope you understand what I want to say. So far I think I’ll have to make a lot of levels and load them :frowning:

So they are just buttons? If each level is distinctly different then yes, you will have to make each level. If the levels are all the same but just have cubes in different spots then you can have one button & then create the level randomly when they press it.

1 Like

This is what I don’t know to do haha :frowning:
Thanks for helping.

It’s late here. I did something like this last year, if I can find the script I will try to post it tomorrow.

1 Like

That would help me a lot! Thanks man!

ok, this probably isnt the most ideal way to do it (I’m still learning) but I think it will do what you want.

1: create your ground (for testing I just used a plane)
2: put in an empty gameobject roughly where you want a cube to spawn. Label this gameobject SpawnPoint or something useful.
3. Copy it as many times as you want & move each of them to where you want.
4: Create another empty gameobject & move it offscreen somewhere. This will be sort of like your manager for your spawn points.
5: Attach this script to it.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SpawnCubes : MonoBehaviour
{

    public List<Transform>spawnSpots = new List<Transform> ();
    //List <SpawnCubes> cubes = new List<SpawnCubes>();
    public int cubeCount = 5;


    public GameObject cubePrefab;


    // Use this for initialization
    void Start ()
    {
   
   
    }

    // Update is called once per frame
    void Update ()
    {
        // manage spawning of cubes
   
        if (CubeScript.cubes.Count < cubeCount) {
            // find a random spawn spot
            int spawnPos;
            bool spotFree;
            do {
                spotFree = true;
                spawnPos = Random.Range (0, spawnSpots.Count - 1);
                // check for cubes on top of each other
                foreach (CubeScript sc in CubeScript.cubes) {
                    if(sc.transform.position == spawnSpots [spawnPos].position)   
                        spotFree = false;
                }
            }
            while(spotFree==false);
       
       
            // instantiate the cube in this spot
            Instantiate (cubePrefab, spawnSpots [spawnPos].position, spawnSpots [spawnPos].rotation);

       
        }

    }
}

6: select the manager object & lock the inspector.
7: select all the spawnpoints (excluding the manager) & drag them into the inspector to the item labelled Spawn Spots. It will create the size of the list & drop in all the spawn points as elements in the list.
8: unlock the inspector.
9: create your cube. Prefab it & attach this script to the prefab

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CubeScript : MonoBehaviour {

    static public List<CubeScript> cubes = new List<CubeScript>();

    // Use this for initialization
    void Start () {
        cubes.Add (this);
    }

    // Update is called once per frame
    void Update () {

    }
}

10: Drag the cube prefab into the Spawn Holder in the label Cube Prefab
11: Save everything.
12: Do a whole stack of tests to make sure that 5 items are spawning & visible on screen (in case it still spawns items on top of each other).
13: In the inspector for the spawn manager there is a field for Cube Count. This is set in the cube spawn script & is public so you can change it in the inspector. I tested this with 18 spawn points & 5 cubes as it was nice & small to test with.

What the scripts do:
the CubeScript just creates a list & inserts each cube into the list when it is instantiated.

the SpawnCubes script does all the work. It holds a list of all the possible spawn points. When it runs it randomly picks a number from 0 to the count of the number of items in the list -1 (because the list starts at 0, not 1). It then checks if that position is occupied against each cube in the cube list created when the cubes spawn. If something is there it starts again. When it finds an empty spot it instantiates a cube, which in turn updates the list of cubes when it is created.

Hope this helps.

Edit: if I have left mention of trees in the script replace it with cubes, I hacked this from my old script & may have missed bits. I also know my code commenting is bad. Note I left the empty Update on the cubeScript. If the cubes do nothing you can delete it but I left it there in case you did want them doing something when they are on screen (e.g. moving).

Edit Edit: I used c# as I don’t know java & I tested this in unity using 3D objects. Try to replicate this in java if that is what you use as you will learn more. If you decide to just use them make sure you create the script as c#.

1 Like

I had to stop at step 5, I get error “error CS0246: The type or namespace name `CubeScript’ could not be found. Are you missing a using directive or an assembly reference?”

Here’s the problem foreach (CubeScript sc in CubeScript.cubes) {

You have to make sure that the scripts you create are called exactly the same as I named mine, same capitalisation etc. in unity create a c# script called SpawnCubes then in the code screen highlight everything, delete it & then paste every line from my SpawnCubes script (make sure you don’t miss the last set of braces, clicking in the code section & using Ctrl-a will select everything). Repeat for the CubeScript one, making sure the script you create in unity is spelt the same way.

Namespace errors usually indicate missing braces, it’s trying to say that the for loop is the same as void update() for example, so that means that you are missing braces & the for loop has lined up as a function instead of just a for loop.

1 Like

I think I’ll go by doing level by level.
I want to say you huge thank you for trying to help me! Thanks man :wink:

No worries, good luck with it

1 Like