- Home /
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);
}
}
}
Answer by TBruce · Aug 05, 2016 at 01:11 AM
Have the instantiated objects deleted on game pause.
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);
}
}
}
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
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
Thank you, I've tried what you suggested but it's still spawning in instantiate clones when the game over panel is shown
Answer by akrot · Aug 03, 2016 at 01:44 AM
What about:
public void PauseGame()
{
Time.timeScale = 0;
}
public void UnPauseGame()
{
Time.timeScale = 1;
}
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
Follow this Question
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