-1

Im making a terror game, and i want to spawn little collectibles in my scene, my code works fine, but they repeat the world location in every instantiate:

using UnityEngine;

public class objectivesGeneration : MonoBehaviour
{
    GameObject[] objSpawn;
    GameObject objActual;
    public GameObject obj;
    int index;
    void Start()
    {
        objSpawn = GameObject.FindGameObjectsWithTag("spawnObj");
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.I))
        {
            generateNewObj();
        }
    }

    public void generateNewObj()
    {
        index = Random.Range(0, objSpawn.Length);
        objActual = objSpawn[index];
        createObj();
    }
    public void createObj()
    {
        
        Instantiate(obj, objActual.transform.position, objActual.transform.rotation);
    }
}

Can somebody help me?

3 Answers3

0

You are spawning the object in the same position objActual.transform.position

You should set the limits of the spawn with 2 variables, for example:

public float startx;
public float endx;
public float starty;
public float endy;

Then you can easily Instantiate randomly in this positions:

float posX = Random.Range(startx, endx);
float posy = Random.Range(starty, endy);
Instantiate(obj, new Vector3(posx, posy, transform.position.z, Quaternion.Identity);

And so you have a random spawn!

Leoverload
  • 1,194
  • 3
  • 8
  • 21
0

Your question can be understood in two ways. Leoverload gave the answer to the repeating position. In case you do not want to repeat the same type of object (so all objects are unique), then do the following:

  1. Turn objSpawn into a List<GameObject> variable and in the start() function, add all instances from the array that's returned from FindGameObjectsWithTag to it.
  2. Turn objSpawn.Length into objSpawn.Count (which does the same but for lists) In that same function add: objSpawn.Remove(objActual) at the end. If those objects are destroyed at some point and you want to create new instances of destroyed objects, ensure that in their Destroy event, you Add(gameObject) to the list again.

Am giving you instructions instead of just code so you can learn to do this yourself in the future.

Also I have the feeling you need to learn how arguments\parameters work. Then you can ommit that objActual variable and instead pass the object to createObj(GameObject theObj) directly.

AlexGeorg
  • 967
  • 1
  • 7
  • 16
0

Thanks to AlexGeorg for the answer, this was how my final code was

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

public class objectivesGeneration : MonoBehaviour
{
    public GameObject recipient;

    public List<GameObject> objSpawns = new List<GameObject>();
    int spawnSelected;


    void Start()
    {
        //Set all the objs with the tag spawnLocation inside of the list
        foreach (GameObject OBJ in GameObject.FindGameObjectsWithTag("spawnLocation"))
        {
            objSpawns.Add(OBJ);
        }
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.I))
        {
            //Check if there are more possible spawns
            if (objSpawns.Count > 0)
            generateNewObj(recipient);
        }
    }

    public void generateNewObj(GameObject frasco)
    {
        //Get a random number of the list which contains one obj
        spawnSelected = Random.Range(0, objSpawns.Count);
        //Instantiate the GameObject in the location of the random selected object
        Instantiate(frasco, objSpawns[spawnSelected].transform.position, objSpawns[spawnSelected].transform.rotation);
        //Delete that possible location from the list
        objSpawns.RemoveAt(spawnSelected);
    }
}