I created an empty object and put two 2d object in that, if the player touches one of the two object it runs this code for that object. A script is attached to the player that runs the code below.
gameObject.SetActive(false);
In the script below is how i’m spawning those object and object pooling them. So that when they leave the screen they go back so then can keep going though the scene forever until the game is over, like in flappy bird.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPooling : MonoBehaviour {
public int objectPoolSize = 5;
public GameObject objectPrefab;
public float spawnRate = 4f;
public float objectMin = -1f;
public float objectMax = 3.5f;
private GameObject[] objects;
private Vector2 objectPoolPosition = new Vector2 (-15f, -25f);
private float timeSinceLastSpawned;
private float spawnXPosition = 10f;
private int currentObject = 0;
// Use this for initialization
void Start () {
objects = new GameObject[objectPoolSize];
for (int i = 0; i < objectPoolSize; i++) {
objects *= (GameObject)Instantiate (objectPrefab, objectPoolPosition, Quaternion.identity);*
-
}*
-
}*
-
// Update is called once per frame*
-
void Update () {*
-
timeSinceLastSpawned += Time.deltaTime;*
-
if (GameControl.instance.gameOver == false && timeSinceLastSpawned >= spawnRate) {*
-
timeSinceLastSpawned = 0;*
-
float spawnYPosition = Random.Range (objectMin, objectMax);*
-
objects [currentObject].transform.position = new Vector2 (spawnXPosition,spawnYPosition);*
-
currentObject++;*
-
if (currentObject >= objectPoolSize) {*
-
currentObject = 0;*
-
}*
-
}*
-
}*
}
I cant figure out where to put a setActive(true); for my objects, when I play the game if i touch an object it disappears like I want but I cant get it to return for when it loops through.