Problem with checking multiple objects are active.
I try to make a simple game when you have to click all the objects on scene. When you click the object, it become not active. I tried to make a script that checks if all objects are not active, but it don't work and i don't have idea why. (Sorry for bad English)
void Update()
{
if (objectsFound == winObjects.Length)
{
Debug.Log("Win");
}
}
private void OnMouseDown()
{
for (int i = 0; i > winObjects.Length; i++)
{
if (winObjects[i].active == false)
{
objectsFound++;
Debug.Log(objectsFound);
}
else
{
objectsFound = 0;
}
}
}
}
on line 11 shouldn't it be for(int i=0;i< winObjects.Length;i++)
The objectsFound logic seems like it won't work but try it might work.
Answer by andrew-lukasik · Aug 09, 2021 at 09:35 AM
First, add this script to all those "winObjects":
WinObject.cs
using System.Collections.Generic;
using UnityEngine;
public class WinObject : MonoBehaviour
{
public static List<WinObject> Instances = new List<WinObject>();
void OnEnable ()
{
Instances.Add( this );
}
void OnDisable ()
{
Instances.Remove( this );
}
}
Then, in your main script:
void Update ()
{
if( WinObject.Instances.Count==0 )
Debug.Log("Win");
}
Your answer
Follow this Question
Related Questions
How to check if player is rapidly rotating the joystick? 2 Answers
make a system for me 0 Answers
Anyone wanna help? 0 Answers
C# - JS problem. 1 Answer
Expanding onto the 2D RougeLike Game adding a main menu (URGENT! help required please) 1 Answer