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 /
  • Help Room /
This question was closed Mar 31, 2017 at 05:04 PM by Angeldfz for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Angeldfz · Mar 31, 2017 at 12:04 PM · prefabdestroyinstance

Destroying an instance of a prefab not working

I have written a simple pause script that creates instances of prefabs for the buttons.

The issue is that when I toggle the menu off the instances are not being destroyed.

Here is my code:

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.UI;
  
  public class PauseScript : MonoBehaviour {
  
      public bool paused = false;
      public GameObject pauseAlert;
      public GameObject playBut;
      public GameObject retryBut;
      public GameObject closeBut;
  
      private GameObject pauseInst;
      private GameObject playInst;
      private GameObject retryInst;
      private GameObject closeInst;
  
      // Update is called once per frame
      void Update () {
          if (Input.GetKeyDown(KeyCode.Escape))
              paused = togglePause();
      }
  
      bool togglePause()
      {
          if (Time.timeScale == 1f)
          {
              Time.timeScale = 0;
              pauseInst = Instantiate(pauseAlert, new Vector3(0, 1, 0), transform.rotation);
              playInst = Instantiate(playBut, new Vector3(1, -2, 0), transform.rotation);
              retryInst = Instantiate(retryBut, new Vector3(0, -2, 0), transform.rotation);
              closeInst = Instantiate(closeBut, new Vector3(-1, -2, 0), transform.rotation);
              return (true);
          }
          else
          {
              Destroy(pauseInst);
              Destroy(playInst);
              Destroy(retryInst);
              Destroy(closeInst);
              Time.timeScale = 1f;
              return (false);
          }
      }
  }

Any help with this would be greatly appreciated, and thanks in advance.

Comment
Add comment · Show 3
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 hexagonius · Mar 31, 2017 at 01:29 PM 0
Share

i can't see an error. you sure you reach the Destroys??

avatar image The_Magical_Kiwi · Mar 31, 2017 at 01:39 PM 0
Share

looks like it should work.

Try DestroyImmediate ins$$anonymous$$d. I'm not sure it will help but it's worth trying.

As hexagonius says, make sure your code is getting to the else block by putting a Debug.Log("I'm in my else block");in there.

Also, I'd change if (Time.timeScale == 1f) to if (!paused) because Time.timescale is a float and as a result it isn't a good idea to compare it to an exact number because rounding errors can cause false negatives.

avatar image Angeldfz · Mar 31, 2017 at 05:03 PM 0
Share

Ah, I've found the issue, it was my error.

This script was working... when I press escape (as it should), but when hitting the play button, it just wasn't destroying from that script, so I'm a doink for overlooking that... xD

Sorry guys. goes back to hiding under a rock

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

113 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

Related Questions

How to destroy GameObject if it overlaps another GameObject 2 Answers

Script won't work on duplicates/prefab copies until I delete Unity meta files 1 Answer

Why Original GameObject Destroy Clones ? 0 Answers

I need help destroying a clone on function. 1 Answer

How do i collect items without destroying or disabling the prefab? 0 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