Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mechanicarts · Apr 11, 2013 at 06:37 AM · prefabdestroypermanent

How to permanently destroy an instance?

I have 5 puzzle pieces in my game. They are instances of the same prefab. When the player walks over any of them, it gets destroyed and +1 is added to my puzzleCounter. However, when you are taken to the menu and then back to the game, the puzzles are there again (although the counter displays the number of puzzle pieces you have found).

How to permanently destroy an instance of a prefab, making sure it never spawns again until the game is restarted?

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

1 Reply

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

Answer by fafase · Apr 11, 2013 at 06:45 AM

When loading a scene, all variables are destroyed. So, when you finish your scene and get to the menu everything is lost. But I guess your counter is a static variable, meaning it survives destruction.

In order to "remenber" what is destroyed, you could use this static variable to know how many pieces should be created on Load. But that draws one issue, if you play the game, finish the level, close the game and start again, your puzzle is not remembered.

Best way would be to look at PlayerPrefs. You could then save the different states of your game and next time you play or when you go back to the menu, it is all remembered.

PlayerPrefs is a little bit like having a database on your local computer.

When you finish the level you use:

 PlayerPrefs.SetInt("RemainingPieces", 0); 

when you start the next time:

 void Start(){
     int pieces = PlayerPrefs.GetInt("RemainingPieces");  
 }

http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html

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 mechanicarts · Apr 11, 2013 at 06:48 AM 0
Share

But, how about their position? Do I have to name them so that I know each one's position? They are not dynamically placed.

avatar image fafase · Apr 11, 2013 at 07:19 AM 0
Share

You probably have an array of position to place them then you can use a for loop. Note that it makes it easier if you actually store the destroyed pieces:

 Vector3 pos[] = new Vector3[size];
 //Here you assigned the position to the array
 int pieces = PlayerPrefs.GetInt("DestroyedPieces");  
 for (int i = size-1 ; i>pieces-1;i--){
    Instantiate(prefab, pos[i],rotation)
 }

That is working if you only can destroy them in order. If you are able to destroy them randomly then you would have to store the state of each I guess.

 PlayerPrefs.SetInt("FirstPiece",0); // Destroyed
 PlayerPrefs.SetInt("FirstPiece",1); // Not destroyed

then in the Start

 Vector3 pos[] = new Vector3[size];
 int [] arr = new int[size];
 arr[0] = PlayerPrefs.GetInt("FirstPiece");
 arr[1] = PlayerPrefs.GetInt("SecondPiece");
 // and so on
 //Here you assigned the position to the array
 int pieces = PlayerPrefs.GetInt("DestroyedPieces");  
 for (int i = 0 ; i< size;i++){
    if(arr[i]== 1)
       Instantiate(prefab, pos[i],rotation)
 }

The actual best way would be to use the UnitySerializer that allows you easily to store object information

http://whydoidoit.com/unityserializer/

But what I give there is an "easy" way to start with.

avatar image mechanicarts · Apr 11, 2013 at 08:34 AM 0
Share

It was getting too complicated for this game (a simple assignment) so I ended up going the ghetto way, a boolean for each piece, it is set to false when collecting it, when the scene starts it checks the boolean, if false it destroys it. Quick and dirty.

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

11 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

Related Questions

Destroying an instantiated prefab particle effect 4 Answers

How to destroy all gameobjects active in hierarchy? 1 Answer

How to Play 3D Sound and Keep Script on Multiple Prefabs after Destroying a gameObject? 0 Answers

How to delete an instantiated object 0 Answers

Destroying Prefab Also Destroys Prefab Reference 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