Sure thing!
The scene is set up with a player capsule scripted to move on a plane. I have an empty game object holding 2 scripts: the Grid Instantiate script and the Respawn script. I have a cube prefab set up to be instantiated by the script.
When the scene plays, the cube shows up in a 5 x 5 grid and the cubes can be picked up by the player. Once they are picked up, I have the script set to spawn a new cube after 1 second. I was hoping to have the cube spawn in the same place it was in the grid, but it doesn’t.
The cubes spawn, but in 5 separate places with 10 cubes placed in one spot, making it look like there is only 1 cube in the scene. When you pick up this cube, it picks up all 10 cubes in the same place and respawns all 10 in the same place.
My goal is to get the cubes to either spawn back in the grid position or to spawn in random places on the stage.
Here’s my most current code:
Player:
public class Player : MonoBehaviour
{
public float movementSpeed = 5.0f;
CharacterController character;
public GameObject prefab;
public int numberOfObjects = 5;
public float radius = 5f;
bool PickedUp = false;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//Get the Character Controller//
CharacterController character = GetComponent<CharacterController>();
//Character Movement//
float FBSpeed = Input.GetAxis("Vertical") * movementSpeed; //forward and back movement
float LRSpeed = Input.GetAxis("Horizontal") * movementSpeed; //left and right movement
Vector3 speed = new Vector3(LRSpeed, 0, FBSpeed); //get the Vector for the speed
speed = transform.rotation * speed; //storing the speed
character.Move(speed * Time.deltaTime); //get the character movement speed
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag =="Pickup")
{
PickedUp = true;
other.gameObject.SetActive(false);
StartCoroutine(RespawnItem());
}
}
IEnumerator RespawnItem ()
{
if(PickedUp)
{
PickedUp = true;
int respawnTime = 1;
yield return new WaitForSeconds(respawnTime);
for (int i = 0; i < numberOfObjects; i++)
{
float angle = i * Mathf.PI * 2 / numberOfObjects;
Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
Instantiate(prefab, pos, Quaternion.identity);
}
Debug.Log ("Spawned!");
}
PickedUp = false;
}
}
Pickup:
public class Pickup : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag =="Pickup")
{
Destroy(other);
}
}
}
Grid setup:
public class GridInstantiate : MonoBehaviour
{
public GameObject prefab;
public float gridX = 5f;
public float gridY = 5f;
public float spacing = 2f;
void Start() {
for (int y = 0; y < gridY; y++) {
for (int x = 0; x < gridX; x++) {
Vector3 pos = new Vector3(x, 0, y) * spacing;
Instantiate(prefab, pos, Quaternion.identity);
}
}
}
}
Circle setup (for testing to make sure I did the instantiate correctly)
public class CircleInstantiate : MonoBehaviour {
public GameObject prefab;
public int numberOfObjects = 20;
public float radius = 5f;
void Start() {
for (int i = 0; i < numberOfObjects; i++) {
float angle = i * Mathf.PI * 2 / numberOfObjects;
Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
Instantiate(prefab, pos, Quaternion.identity);
}
}
// Update is called once per frame
void Update () {
}
}
Respawn:
public class Respawn : MonoBehaviour
{
public GameObject prefab;
public int numberOfObjects = 5;
public float radius = 5f;
bool PickedUp = false;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag =="Pickup")
{
other.gameObject.SetActive(false);
StartCoroutine(RespawnItem());
}
}
IEnumerator RespawnItem ()
{
if(PickedUp)
{
PickedUp = true;
int respawnTime = 1;
yield return new WaitForSeconds(respawnTime);
for (int i = 0; i < numberOfObjects; i++)
{
float angle = i * Mathf.PI * 2 / numberOfObjects;
Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
Instantiate(prefab, pos, Quaternion.identity);
}
Debug.Log ("Spawned!");
}
PickedUp = false;
}
}