- Home /
How to load a new level after the player fixes all the robots in the scene?
Hello! I have a level where the player has to throw cogs at some robots to fix them. I want to load the next level as soon as the player fixes all the robots in the scene. I thought about making an int variable counter and adding +1 to the variable in the Fixed() function, and then in Update checking if the counter has reached the number of robots in the scene. But this doesn't seem to work.
I thought also about making some kind of function that checks if all the robots in the scene are playing the "Fixed" animation. But I have no idea how to do it.
Can anyone help me? This is my script for the robot:
using UnityEngine; using UnityEngine.SceneManagement;
public class Enemy : MonoBehaviour { public float speed; public float timeToChange; public bool horizontal;
public GameObject smokeParticleEffect;
public ParticleSystem fixedParticleEffect;
public AudioClip hitSound;
public AudioClip fixedSound;
Rigidbody2D rigidbody2d;
float remainingTimeToChange;
Vector2 direction = Vector2.right;
bool repaired = false;
// ===== ANIMATION ========
Animator animator;
// ================= SOUNDS =======================
AudioSource audioSource;
void Start ()
{
rigidbody2d = GetComponent<Rigidbody2D>();
remainingTimeToChange = timeToChange;
direction = horizontal ? Vector2.right : Vector2.down;
animator = GetComponent<Animator>();
audioSource = GetComponent<AudioSource>();
}
void Update()
{
if(repaired)
return;
remainingTimeToChange -= Time.deltaTime;
if (remainingTimeToChange <= 0)
{
remainingTimeToChange += timeToChange;
direction *= -1;
}
rigidbody2d.MovePosition(rigidbody2d.position + direction * speed * Time.deltaTime);
animator.SetFloat("ForwardX", direction.x);
animator.SetFloat("ForwardY", direction.y);
}
void OnCollisionStay2D(Collision2D other)
{
if(repaired)
return;
RubyController controller = other.collider.GetComponent<RubyController>();
if(controller != null)
controller.ChangeHealth(-1);
}
public void Fix()
{
animator.SetTrigger("Fixed");
repaired = true;
smokeParticleEffect.SetActive(false);
Instantiate(fixedParticleEffect, transform.position + Vector3.up * 0.5f, Quaternion.identity);
//we don't want that enemy to react to the player or bullet anymore, remove its reigidbody from the simulation
rigidbody2d.simulated = false;
audioSource.Stop();
audioSource.PlayOneShot(hitSound);
audioSource.PlayOneShot(fixedSound);
}
}
Answer by joemane22 · Mar 09, 2020 at 01:52 AM
Use this code to count the number that are not fixed.
You need using System.Linq
FindObjectsOfType<Enemy>().Count((robot) => !robot.fixed && robot.gameobject.isActive);
This will return the number of existing enemies that are active and not fixed.
You could also do this same thing but maintain your own list of active and not fixed enemies in a static variable like this.
static List<Enemy> unfixedRobots = new List<Enemy>();
void OnEnable()
{
unfixedRobots.Add(this);
}
And when it is fixed just call unfixedRobots.Remove(this)
The latter is probably the best solution so you dont have to find then iterate every frame to check you can just use unfixedRobots.Count to check.
Hello! Sorry to bother you again, but now I have another problem. In Unity play mode the game works fine, after the player fixes the robots, there's an image that appears on screen. But when I build it, after all the robots are fixed, the image that is supposed to appear on screen doesn't show up.
Do you have any idea what could be causing this error?
Some of the common problems are the screen resolution change makes the sprite change in a way you cant see it.
Also it could be that the image is being drawn before another object that then draws over top of it.
Put the image in the UI layer and anything else in a layer before that. Also make sure the anchors are set to stretch all or at least in a way it will stretch or move in a way that doesn't make it dissapear.
Also if you are satisfied with the answer please accept it for me thanks!
Hello, again! So it turns out it's not a problem with my image. I changed the code so that after the player fixes the robots, the next scene is simply loaded. This works in play mode in Unity but again in the build it doesn't work. So it's like in the build the code that counts the unfixed robots doesn't work anymore. I am really not sure why. Any ideas? And thank you so much again!
Answer by ciupyloiii · Mar 09, 2020 at 01:55 PM
Thank you so much for your help!!! It worked! I am still pretty new to Unity and C# and I have a lot to learn.
Your answer

Follow this Question
Related Questions
Load next level after reaching a number 1 Answer
Run time counter 1 Answer
Only tagged objects raise counter 1 Answer
Making something happen only for one second during an Update function? 1 Answer
Creating an up and down counter 2 Answers