Question by
Lord_Arcanis · Mar 18, 2017 at 12:50 AM ·
c#setactive
Can't deactivate activated object
near the bottom there is code to set one of the active rows to false if it is behind the camera (there is another script for the camera to follow the player and for the player to move) but for some reason it won't deactivate the rows that are behind the cameras range. Does anyone have any idea why and a possible solution? Thanks
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, 4.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].SetActive (false);
}
}
for(int i = 0; i < row2.Count; i++){
if (row2 [i].transform.position.x < back) {
row2 [i].SetActive (false);
}
}
for(int i = 0; i < row3.Count; i++){
if (row3 [i].transform.position.x < back) {
row3 [i].SetActive (false);
}
}
for(int i = 0; i < row4.Count; i++){
if (row4 [i].transform.position.x < back) {
row4 [i].SetActive (false);
}
}
}
}
Comment