'Why can't gameobject in the hierarchy find it without problems?
I want to find the GameObject that is in the hierarchy correctly in the hierarchy. But this does not happen correctly, so I want 'if' to work when the gameobject I have tagged is active, but 'if' works even though there is no other gameobject tag, which is why this problem is caused.
Note: My level of English is not good, I try to explain the problem to you as much as I can, please tell me if there is anything you do not understand, I will try to explain it in detail as much as I can. Thank you for your understanding.
- Image tagged gameobject: enter image description here
- The point where the problem occurred: enter image description here
- Gameobject tag name: enter image description here
- Cubes Gameobject: enter image description here
void Update()
{
if (cubes.instance.objPool.Count == 1) //It works when the number of gameobject in the list is 1 when I do a random deletion.
{
RewardSystem();
}
}
public void RewardSystem()
{
GameObject FoundCube = GameObject.FindGameObjectWithTag(cube_Tag_Number);
if (FoundCube == null)
{
SoundManager.instance.GameOverMenuSound();
game_Over_Menu.SetActive(true);
next_Level_Menu.SetActive(false);
confetti.Stop();
clickButton.gameObject.SetActive(false);
missionButton.gameObject.SetActive(false);
enabled = false;
}
if (FoundCube != null)// problematic if
{
if (FoundCube.activeInHierarchy == true) // problematic if
{
SoundManager.instance.NextLevelMenuSound();
next_Level_Menu.SetActive(true);
confetti.Play();
game_Over_Menu.SetActive(false);
clickButton.gameObject.SetActive(false);
missionButton.gameObject.SetActive(false);
enabled = false;
}
}
}
public class cubes : MonoBehaviour
{
public static cubes instance;
public int randomRangeNumber1, randomRangeNumber2;
public List<GameObject> objPool = new List<GameObject>();
private void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != null)
{
Destroy(gameObject);
}
}
public void ButtonOnDown()
{
SoundManager.instance.LuckButtonSound();
for (int i = 0; i < 1; i++)
{
int index = Random.Range(randomRangeNumber1, randomRangeNumber2);
GameObject.Destroy(objPool[index]);
objPool.RemoveAt(index);
randomRangeNumber2 = objPool.Count;
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
