Why won't these objects deactivate?
The code on deactivating that I am referencing is at the bottom, It is supposed to deactivate the objects if they are behind the camera, which follows the player. Though when the rows that are activated are behind the camera's point of view or anywhere for that matter just don't disable after I have activated them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RowSpawner : MonoBehaviour
{
public bool isAlive;
float xPos;
Vector3 rowSpot;
public int random;
public GameObject rowOf1;
public GameObject rowOf2;
public GameObject rowOf3;
public GameObject rowOf4;
public int pooledAmount;
public List<GameObject> row1;
public List<GameObject> row2;
public List<GameObject> row3;
public List<GameObject> row4;
public float back;
public float front;
public GameObject cameraUI;
private float cameraPos;
void Start ()
{
row1 = new List<GameObject> ();
row2 = new List<GameObject> ();
row3 = new List<GameObject> ();
row4 = new List<GameObject> ();
isAlive = true;
InvokeRepeating ("Spawner", 2.0F, 2.0F);
xPos = 0;
for (int i = 0; i < pooledAmount; i++) {
GameObject obj1 = (GameObject)Instantiate (rowOf1);
obj1.SetActive (false);
row1.Add (obj1);
GameObject obj2 = (GameObject)Instantiate (rowOf2);
obj2.SetActive (false);
row2.Add (obj2);
GameObject obj3 = (GameObject)Instantiate (rowOf3);
obj3.SetActive (false);
row3.Add (obj3);
GameObject obj4 = (GameObject)Instantiate (rowOf4);
obj4.SetActive (false);
row4.Add (obj4);
}
}
void Update ()
{
cameraPos = cameraUI.transform.position.x;
back = cameraPos - 9.75F;
front = cameraPos + 9.75F;
Destroyer ();
}
void Spawner ()
{
random = Random.Range (1, 5);
xPos = xPos + 8F;
rowSpot = new Vector3 (xPos, 0, 0);
if (isAlive == true) {
if (random == 1) {
for (int i = 0; i < row1.Count; i++) {
if (!row1 [i].activeInHierarchy) {
row1 [i].transform.position = rowSpot;
row1 [i].transform.rotation = Quaternion.identity;
row1 [i].SetActive (true);
break;
}
}
}
if (random == 2) {
for (int i = 0; i < row2.Count; i++) {
if (!row2 [i].activeInHierarchy) {
row2 [i].transform.position = rowSpot;
row2 [i].transform.rotation = Quaternion.identity;
row2 [i].SetActive (true);
break;
}
}
}
if (random == 3) {
for (int i = 0; i < row3.Count; i++) {
if (!row3 [i].activeInHierarchy) {
row3 [i].transform.position = rowSpot;
row3 [i].transform.rotation = Quaternion.identity;
row3 [i].SetActive (true);
break;
}
}
}
if (random == 4) {
for (int i = 0; i < row4.Count; i++) {
if (!row4 [i].activeInHierarchy) {
row4 [i].transform.position = rowSpot;
row4 [i].transform.rotation = Quaternion.identity;
row4 [i].SetActive (true);
break;
}
}
}
}
}
public void Destroyer ()
{
for(int i = 0; i < row1.Count; i++){
if (row1 [i].transform.position.x < back && row1[i].activeInHierarchy) {
row1 [i].SetActive (false);
break;
}
}
for(int i = 0; i < row2.Count; i++){
if (row2 [i].transform.position.x < back && row2[i].activeInHierarchy) {
row2 [i].SetActive (false);
break;
}
}
for(int i = 0; i < row3.Count; i++){
if (row3 [i].transform.position.x < back && row3[i].activeInHierarchy) {
row3 [i].SetActive (false);
break;
}
}
for(int i = 0; i < row4.Count; i++){
if (row4 [i].transform.position.x < back && row4[i].activeInHierarchy) {
row4 [i].SetActive (false);
break;
}
}
}
}
Answer by Mine54i__ · Mar 18, 2017 at 10:40 PM
Excuse me if I do not understand what you are trying to achieve, but from what it sounds like you are trying to disable all of the objects behind the camera to boost performance? If so, I would just use occlusion culling, which is built into Unity. You can find great tutorials and it is so much simpler than creating a script for it. Hope this helps :)
Though it is similar, I am trying to disable items in the object pool if they are moved out of the camera so they can be reused. I need to be able to reuse these objects ins$$anonymous$$d of making more because this is for a mobile game and it needs to be optimized. Instantiating a ton of new objects isn't really great for optimization.
Your answer
Follow this Question
Related Questions
Regarding SetActive function 1 Answer
Disable Object and Enable by Distance from Player 1 Answer
SetActive delay between code and actual object on the screen 0 Answers
My Game Over Canvas is working in one level but not the other! 0 Answers
Unable to reactivate an object that's been deactivated. C# 4 Answers