Hello! I had a couple of questions about my GameManager.
Question 1:
I have a list of GameObjects that consists of every GameObject in the current scene, then it iterates through a for loop to filter out every object that isn’t tagged “Brick”, like so:
public static List<GameObject> find_bricks()
{
Scene curr_scene = SceneManager.GetActiveScene();
List<GameObject> bricks = new List<GameObject>(curr_scene.GetRootGameObjects());
int brick_count = bricks.Count;
for (int j = 0; j < brick_count; j++)
{
if (bricks[j].tag != "Brick")
{
bricks.Remove(bricks[j]);
brick_count = bricks.Count;
j = 0;
}
}
return bricks;
}
The problem:
In every scene, there is always one object that is not filtered. It remains in the list at bricks[0]. Here’s a screenshot of the scene as well as the Debug.Log of the list in the console:
Here’s another to show it’s not just one object, but multiple:
Question 2:
So I’ve been coding in Unity for about 5+ years now and before I show the entire code I wanted to ask for some feedback or anyways to improve my code’s efficiency/formatting etc. Among my circle of friends/connections I’ve always been the go to guy for programming questions, and have never had anyone better than me to mentor me or help me improve upon my skills (Most are artists which I am very far from). So that’s basically what I’m asking of this community. I’ve come here many times, but have rarely ever posted anything on this forum (Mostly because any questions I’ve had have been answered). Thank you very much for your time, and I appreciate this community for all of the help it has given me throughout the years!
Entire Code:
Game_Man(C#):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class Game_Man
{
public static void change_level()
{
Scene curr_scene = SceneManager.GetActiveScene();
int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCountInBuildSettings;
string[] scenes = new string[sceneCount];
int curr_scene_num = 0;
for (int i = 0; i < sceneCount; ++i)
{
scenes *= System.IO.Path.GetFileNameWithoutExtension(UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex(i));*
if (curr_scene.name == scenes*)
_{_
curr_scene_num = i;
_}_
_}*_
curr_scene_num = curr_scene_num + 1;
SceneManager.LoadScene(scenes[curr_scene_num]);
}
public static List find_bricks()
{
Scene curr_scene = SceneManager.GetActiveScene();
List bricks = new List(curr_scene.GetRootGameObjects());
int brick_count = bricks.Count;
for (int j = 0; j < brick_count; j++)
{
if (bricks[j].tag != “Brick”)
{
bricks.Remove(bricks[j]);
brick_count = bricks.Count;
j = 0;
}
}
Debug.Log(bricks[0]);
Debug.Log(bricks[1]);
return bricks;
}
public static IEnumerator check_bricks(List list)
{
while(true)
{
yield return null;
if (list[1].GetComponentInChildren() == null)
{
change_level();
}
}
}
}
brick_man(C#):
using UnityEngine;
public class brick_man : MonoBehaviour
{
private void Start()
{
StartCoroutine(Game_Man.check_bricks(Game_Man.find_bricks()));
}
}