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 /
avatar image
0
Question by Internetman · Mar 10, 2016 at 09:47 AM · c#instantiatearrayerror messageprefabs

Destroy a Prefab from an Array? (C#)

Hey you, yeah you my friend! I have a problem with my (C#) code, I'm trying to delete a random prefab created by an array. I instantiate a random prefab and it spawns out in the scene, the prefab is going to stay in the scene for four seconds, it will then destroy itself. This is where the problem shows up! I try destroying the prefab with help of a loop, but get the error "Destroying assets is not permitted to avoid data loss"

How can I make the prefab destroy itself? Grateful for any help I can get! Thank you! Tried searching for the problem but most of it doesn't work or is in Java script.

 using UnityEngine;
 using System.Collections;
 
 public class SpawnPowerUp : MonoBehaviour 
 {
     // Power up array
     public GameObject[] PowerUpsPrefabs;
     
     // Borders
     public Transform borderTop;
     public Transform borderBottom;
     public Transform borderLeft;
     public Transform borderRight;
 
     //Timer
     public float timer = 4.0f; 
 
     //HasSpawned
     public bool hasPowerUpSpawned = false;
 
     void Start () 
     {
         // Spawn a new power up every 10 seconds, starting in 10
         InvokeRepeating("Spawn", 10, 10);
     }
 
     void Update()
     {
         if(hasPowerUpSpawned == true)
         {
             timer -= Time.deltaTime;
         }
 
         if(timer <= 0.0f)
         {
             for (int i= 0; i< PowerUpsPrefabs.Length; i++)
             {
                 Destroy (PowerUpsPrefabs[i].gameObject); ***//Error here***
             }
         }
     }
     
     // Spawn a Power up
     void Spawn() 
     {
 
         //Random range between every power up
         int index = Random.Range (0, PowerUpsPrefabs.Length);
 
         // x position between left & right border
         int x = (int)Random.Range(borderLeft.position.x,
                                   borderRight.position.x);
         
         // y position between top & bottom border
         int y = (int)Random.Range(borderBottom.position.y,
                                   borderTop.position.y);
         
         // Instantiate the power up at (x, y)
         Instantiate(PowerUpsPrefabs[index],
                     new Vector2(x, y),
                     Quaternion.identity); // default rotation
 
         hasPowerUpSpawned = true;
     }
 
 }




Comment
Add comment · Show 1
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 Ali-hatem · Mar 10, 2016 at 10:03 AM 1
Share

why don't you attach a script to the brefabs which hase one function that destroy brefab after 4s :

 void Start()
 {
 Destroy (gameObject, 4);
 } 

2 Replies

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

Answer by troien · Mar 10, 2016 at 02:27 PM

You actually dont want to destroy the prefab. you want to destroy the instance (note the difference here). As the prefab is located in the project folder, and destroying it would permanently destroy it, Unity realises that this is probably not what you want and throws an error. The instance (the thing you create by calling Instantiate with the prefab as parameter) is the thing you see in your scene/hierarchy, which is a copy of the prefab and which is the thing that you want to destroy. If you want a reference to the instance, Instantiate returns the instance you create so you could use that...

The quick solution here would just be changing line 59 to:

 Destroy(Instantiate(PowerUpsPrefabs[index],
                      new Vector2(x, y),
                      Quaternion.identity), 4f); // Creates an instance and destroyes it after 4 seconds

And probaby getting rid of the Update method entirely as the Object now gets destroyed in another way.

But then It would probably be better to give your prefab a script that destroys itself as Ali hatem suggested, as you can have more control over the conditions of when the object should get destroyed without having to create a list of all your instances.

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 LE0NIDAS · Mar 10, 2016 at 10:11 AM

looks like you are trying to destroy your assets you've stored in PowerUpsPrefabs[]. Instead you should have a reference to the instantiated PowerUpsPrefabs inside the scene and destroy them.

You could e.g. when you instantiate yout Prefabs, save this references to a new array, iterate over this new array to destroy them.

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Instantiation of my GameObjects spell (from other script) 0 Answers

How to instantiate bullets in multiple transforms 1 Answer

Instantiated Prefab won't instantiate with a script reference 1 Answer

Spawning prefabs randomly within a rectangle 1 Answer

How can i get a script of a non instantiate prefab? 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