Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by wasicool7 · Aug 02, 2016 at 11:55 AM · instantiateclonestopinvoke

Unity2D: How do I stop instantiate spawn (clone)?

Hi, so basically I have a game over panel, with an animation attach to it to make the panel slide down when the player loses all of his lives. I created a UI script to help me pause and unpause game objects behind the The game over panel when it comes down. It works but I have enemies that spawns randomly in my game, so the clones aren't being paused when the gamover panel comes down. Does anybody know how to stop cloning object when the game over panel is paused. This is my spawning script: (Javascript)

    #pragma strict

       // Variable to store the enemy prefab
          public var enemy : GameObject;
          public var gameCharacters : GameObject;

       // Variable to know how fast we should create new enemies
          public var spawnTime : float = 2;

               function Start() {  
                 // Call the 'addEnemy' function every 'spawnTime' seconds
                    InvokeRepeating("addEnemy", spawnTime, spawnTime);
                    }

       // New function to spawn an enemy
              function addEnemy() {  
       // Variables to store the X position of the spawn object
       // See image below
              var x1 = transform.position.x - GetComponent.<Renderer>().bounds.size.x/2;
              var x2 = transform.position.x + GetComponent.<Renderer>().bounds.size.x/2;

          // Randomly pick a point within the spawn object
              var spawnPoint = new Vector2(Random.Range(x1, x2), transform.position.y);

          // Create an enemy at the 'spawnPoint' position
               Instantiate(enemy, spawnPoint, Quaternion.identity);
                }


And this is how I pause my game (within my UI script):

   public class UIManagerScript : MonoBehaviour {

 public GameObject gameCharacters;


 public void PauseGame()
 {
     if (gameCharacters != null)
     {
         gameCharacters.SetActive(false);
     }
 }
 
 public void UnPauseGame()
 {
     if (gameCharacters != null)
     {
         gameCharacters.SetActive(true);
        }
      }
  }
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · Aug 05, 2016 at 01:11 AM

Have the instantiated objects deleted on game pause.

Comment
Add comment · Show 1 · 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 wasicool7 · Aug 05, 2016 at 10:57 AM 0
Share

Thank you :D

avatar image
0

Answer by NordSpirit · Aug 02, 2016 at 07:31 PM

Hi @wasicool7. There are many way to do what you want, but is the most simple for me is create new tag for enemy, and call it "Enemy", the change your code a litle :

 // instead of   Instantiate(enemy, spawnPoint, Quaternion.identity);
 var Enemy: GameObject = Instantiate(enemy, spawnPoint, Quaternion.identity);
 Enemy.tag = "Enemy";

And then change your pause script:

            public class UIManagerScript : MonoBehaviour {
     public GameObject gameCharacters;
     public void PauseGame()
     {
         if (gameCharacters != null)
         {
             gameCharacters.SetActive(false);
         }
         GameObject[] Enemys = GameObject.FindGameObjectsWithTag ("Enemy");
         for (int i=0;i<Enemys.Length;i++){
             Enemys[i].SetActive(false);
         }
     }

     public void UnPauseGame()
     {
         if (gameCharacters != null)
         {
             gameCharacters.SetActive(true);
         }
         GameObject[] Enemys = GameObject.FindGameObjectsWithTag  ("Enemy");
         for (int y=0;y<Enemys.Length;y++){
             Enemys[y].SetActive(true);
         }
     }
 }

Comment
Add comment · Show 3 · 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 wasicool7 · Aug 02, 2016 at 07:52 PM 1
Share

Thank you for your answer, but I'm getting all sorts of errors:

spawnScript.js(27,19): UCE0001: ';' expected. Insert a semicolon at the end.

(38,39): error CS1061: Type UnityEngine.GameObject[]' does not contain a definition for Lenght' and no extension method Lenght' of type UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)

(39,32): error CS0103: The name y' does not exist in the current context (50,39): error CS1061: Type UnityEngine.GameObject[]' does not contain a definition for Lenght' and no extension method Lenght' of type UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?) (51,32): error CS0103: The name y' does not exist in the current context

avatar image NordSpirit wasicool7 · Aug 02, 2016 at 10:55 PM 0
Share

Yes, i'am sorry my mistake. Your spawn script use Java, but pause is C#. I don't know java syntax good enaugh. But try this:

 // ins$$anonymous$$d GameObject Enemy = Instantiate(enemy, spawnPoint, Quaternion.identity);
 
 var Enemy: GameObject = Instantiate(enemy, spawnPoint, Quaternion.identity);

and PauseScript:

     public class UI$$anonymous$$anagerScript : $$anonymous$$onoBehaviour {
         public GameObject gameCharacters;
         public void PauseGame()
         {
             if (gameCharacters != null)
             {
                 gameCharacters.SetActive(false);
             }
             GameObject[] Enemys = GameObject.FindGameObjectsWithTag ("Enemy");
             for (int i=0;i<Enemys.Length;i++){
                 Enemys[i].SetActive(false);
             }
         }
 
         public void UnPauseGame()
         {
             if (gameCharacters != null)
             {
                 gameCharacters.SetActive(true);
             }
             GameObject[] Enemys = GameObject.FindGameObjectsWithTag  ("Enemy");
             for (int y=0;y<Enemys.Length;y++){
                 Enemys[y].SetActive(true);
             }
         }
     }


It's should work fine

avatar image wasicool7 NordSpirit · Aug 03, 2016 at 02:26 PM 0
Share

Thank you, I've tried what you suggested but it's still spawning in instantiate clones when the game over panel is shown

avatar image
0

Answer by akrot · Aug 03, 2016 at 01:44 AM

What about:

 public void PauseGame()
 {
    Time.timeScale = 0;
 }
 
 public void UnPauseGame()
 {
    Time.timeScale = 1;
 }




Comment
Add comment · Show 1 · 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 wasicool7 · Aug 03, 2016 at 11:46 AM 0
Share

Thank you I tried that idea before, but it was pausing my everything including my timer countdown so that's why I created a game Object called gameCharacters. So that it will only pause things that is a child under the game object. but unfortunately it doesn't pause the instantiate clones.

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Instantiate Reference Problem 1 Answer

Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers

Following Object can't find newly Instantiated Object (Solved) 1 Answer

Destroy a specific instantiated clone? 2 Answers

When cloning, how do you make your clones keep the original properties? 2 Answers


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