• 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 pinkanna · Jul 05, 2014 at 05:56 PM · c#gameobjectinstantiateprefabdestroy

Trouble with destroying an instantiated prefab

Hi guys, I made a pause script. In that script I want to have my prefab (which is just a transparent png image/sprite saying pause) appear when the esc button is pressed and disappear when the escape button is pressed again.

I have everything else working to stop the game. The prefab does appear when I press pause... but it seems when I try to unpause, the prefab still remains. And when I pause again it just overlays the previous prefab.

I am still learning this whole thing, so I apologize in advance if it's something simple. Any advice on how to fix this or a better way to implement is appreciated. Thanks

PauseScript

 using UnityEngine;
 using System.Collections;
 
 
 
 public class PauseScript : MonoBehaviour {
 
     public bool CanPause;
 
     void Start () {
         CanPause = true;
     
 
     }
     void Update () {
 
         if (Input.GetKeyDown (KeyCode.Escape)) {
             GameObject newPause = (GameObject)Instantiate(Resources.Load("Pause"));
             if(CanPause)
             {
                 Debug.Log("pause");
                 Time.timeScale=0;
                 //GameObject Pause = (GameObject)Instantiate(Resources.Load("Pause"));
                 audio.pitch = Time.timeScale = 0;
                 CanPause = false;
 
             }
             else
             {
                 Time.timeScale=1;
                 GameObject.Destroy(newPause);
                 audio.pitch = Time.timeScale = 1;
                 CanPause=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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by behzad.robot · Jul 05, 2014 at 06:26 PM

Why r u even trying to do it like this U could use gui objects in ur code instead(much better than dealing with gameobjects)and i'd never seen someone instantiating an object like that (As long as i know Usually people use a public variable!)And man i think the problem is inside the if (Input.GetKeyDown (KeyCode.Escape)) u create a new pause once the button is pressed not caring whether there is already one or not!Try Defining the Variable of newPause outside this if and Putting an instantiate of ur pause screen inside if(CanPause) :D Like this:

/ I'm not really good in c# But i think the problem is where u define this and where u put it's value nothing to do with my c# syntax knowledge/

   GameObject newPause //Defining this variable as a private one outside Update :D
  void Update () {
         
         if (Input.GetKeyDown (KeyCode.Escape)) {
           
             if(CanPause)
             {
                 newPause = (GameObject)Instantiate(Resources.Load("Pause")); 

// u c putting this here :D

                 Debug.Log("pause");
                 Time.timeScale=0;
                 //GameObject Pause = (GameObject)Instantiate(Resources.Load("Pause"));
                 audio.pitch = Time.timeScale = 0;
                 CanPause = false;
  
             }
             else
             {
                 Time.timeScale=1;
                 GameObject.Destroy(newPause);
                 audio.pitch = Time.timeScale = 1;
                 CanPause=true;
             }
         }
     }

Comment
Add comment · 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
0

Answer by pinkanna · Jul 09, 2014 at 02:16 AM

Sorry for taking so long to respond. This works. Thank you very much!

Comment
Add comment · 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

22 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

PlayerRespawn class wont Instantiate the player prefab 1 Answer

Destorying prefab and instanstiating again? 0 Answers

Thrown objects always have same rotation 2 Answers

How to make a gameObject instantiate a prefab for every 250 damage taken ? 4 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