• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by ciupyloiii · Mar 08, 2020 at 10:02 AM · counterload level

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);
 }

}

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
ciupyloiii

People who like this

1 Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ciupyloiii · Mar 09, 2020 at 09:25 PM 0
Share

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?

avatar image joemane22 ciupyloiii · Mar 09, 2020 at 10:59 PM 0
Share

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.

avatar image joemane22 ciupyloiii · Mar 09, 2020 at 11:57 PM 0
Share

Also if you are satisfied with the answer please accept it for me thanks!

avatar image ciupyloiii joemane22 · Mar 10, 2020 at 09:38 AM 0
Share

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!

Show more comments
avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Load next level after reaching a number 1 Answer

Resouce script help 3 Answers

GuiText Score On Mouse Click 1 Answer

Roguelike Elements... 1 Answer

Counter problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges