Hi, I am trying to achieve this script that allows me to change the position of my assets stored in an array variable called pollenSpores into 4 other positions out of 8 possible ones in a random manner. As in each GameObject going to a different location.
The code I currently have allows me to teleport the 4 objects to the same 4 spawn locations all the time. However I would like those spawn locations to be different/change each time within an array of 8 locations and I am not seeing the way of how to do so, maybe you do?
Here a picture of how it looks in the Engine:
[152467-screenshot-14.jpg*_|152467]
Here my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public int pollenHidden = 0;
public Transform[] spawnLocations;
public GameObject[] prefabToSpawn;
private GameObject[] pollenSpores;
void Start()
{
Spawn();
}
void Update()
{
if (pollenHidden == 4)
{
pollenHidden = 0;
TeleportToOrigin();
}
}
void Spawn()
{
prefabToSpawn[0] = Instantiate(prefabToSpawn[0], spawnLocations[0].transform.position, Quaternion.Euler(0, 0, 0));
prefabToSpawn[1] = Instantiate(prefabToSpawn[1], spawnLocations[1].transform.position, Quaternion.Euler(0, 0, 0));
prefabToSpawn[2] = Instantiate(prefabToSpawn[2], spawnLocations[2].transform.position, Quaternion.Euler(0, 0, 0));
prefabToSpawn[3] = Instantiate(prefabToSpawn[3], spawnLocations[3].transform.position, Quaternion.Euler(0, 0, 0));
}
void TeleportToOrigin()
{
pollenSpores = GameObject.FindGameObjectsWithTag("Pollen");
for (int i = 0; i < pollenSpores.Length; i++)
{
pollenSpores_.transform.position = spawnLocations*.transform.position;*_
}
}
}
_*
I would approach this using a ‘pool’ of locations that you randomly pick from and then remove from the pool. This way you’re guaranteed to not pick the same location twice. I wrote some example code that should demonstrate what I mean. You’ll have to modify your TeleportToOrigin() method as well but this should give you an idea of how to go about it.
It also looks like right now you’re overwriting your prefab list with the instantiated versions which seems like a bug, in the sample code I instead store the instantiated object in a list of game objects.
private List<GameObject> pollenSpores = new List<GameObject>();
private void Spawn()
{
// Initialize a pool of spawn locations from the full list
List<Transform> spawnPool = new List<Transform>(spawnLocations);
// Spawn 4 spores
for (int i = 0; i < 4; ++i)
{
// Get next random spawn location from pool and then remove it from the list
Transform spawnLocation = spawnPool[Random.Range(0, spawnPool.Count)];
spawnPool.Remove(spawnLocation);
pollenSpores.Add(Instantiate(prefabToSpawn*, spawnLocation.position, Quaternion.identity));*
}
}
Hi @phosphoer thank you for your answer! After trying your solution I realized I didn’t explain myself properly on my query. What I am trying to do is to spawn the objects just once and then reuse them (by turning false the Sprite renderer and the simulation of the Rigidbody). What I would like to do then is to store the spawned prefabs in an array or a list, I don’t know what difference makes
and to teleport each of them to a new location out of 8 possible ones after a counter of pollenHidden equals 4. So the code you shared allows the spawn to be random and it’s great! But that just happens once at the Start of the scene and I would like it to also happen when I change the position of the GameObjects before turning them visible.
Thanks to your code I managed to extrapolate a solution that works like the following attached code, although I don’t know if it’s the most correct thing, it’s working!
Here is how it looks:
void Update()
{
if (pollenHidden == 4)
{
pollenHidden = 0;
EnableGameObjectRenderers();
TeleportToOrigin();
}
}
void Spawn()
{
List<GameObject> pollenSpores = new List<GameObject>();
List<Transform> spawnPool = new List<Transform>(spawnLocations);
for (int i = 0; i < 4; ++i)
{
Transform spawnLocation = spawnPool[Random.Range(0, spawnPool.Count)];
spawnPool.Remove(spawnLocation);
pollenSpores.Add(Instantiate(prefabToSpawn*, spawnLocation.position, Quaternion.identity));*
}
}
void TeleportToOrigin()
{
List pollenSpores = new List();
foreach (GameObject pollen in GameObject.FindGameObjectsWithTag(“Pollen”))
{
pollenSpores.Add(pollen);
}
List spawnPool = new List(spawnLocations);
for (int i = 0; i < 4; ++i)
{
Transform spawnLocation = spawnPool[Random.Range(0, spawnPool.Count)];
spawnPool.Remove(spawnLocation);
pollenSpores*.transform.position = spawnLocation.transform.position;*
}
}
void EnableGameObjectRenderers()
{
foreach (GameObject pollen in GameObject.FindGameObjectsWithTag(“Pollen”))
{
Renderer renderer = pollen.GetComponent();
Rigidbody2D rigidBody = pollen.GetComponent();
if (renderer != null)
{
renderer.enabled = true;
}
if(rigidBody != null)
{
rigidBody.simulated = true;
}
}
}