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 Jammer3000 · Feb 26, 2014 at 05:34 AM · javascriptdestroyassets

Destroying assets is not permitted to avoid data loss.?

When I try and use Destroy(); in my C# script it gives me the error Destroying assets is not permitted to avoid data loss. But destroy works in any of my javascript scripts? Here's the C# script I'm trying to use it in.

 using UnityEngine;
 using System.Collections;
 
 public class obstacle_generation : MonoBehaviour {
     
     public GameObject obstacle;
     float x = 0;
     float z = -3.802516f;
     
     void Update () {
         float y = Random.Range(3.042949f, 6.350356f);
         if(x < 10) {
             Instantiate(obstacle, new Vector3(x * 7.5f, y, z),Quaternion.identity);
             x++;
         }
 
         if (obstacle.transform.position.x > -8) {
 
             Destroy(obstacle);
 
         }
     }
 }
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 robertbu · Feb 26, 2014 at 06:13 AM 0
Share

@Jammer3000 - you asked me to take a look at this. @GambinoInd is on the right track, though there is some open questions here that don't allow me to answer. You are creating 10 copies of your prefab. It appears that you want to destroy them if their position is less than -8 (you say greater but that would result in immediate destruction)? But you are referencing the prefab in your destroy, not any of the copies. That is why you are getting the error. You could build a list and check the positions of all the created objects in the list each frame, but a better solution would be to put a script on the object that is being used as a prefab and allow the spawned objects to destroy themselves. Something like:

void Update() { if (transform.position.x < -8) { Destory(gameObject); } }

Note I've change the '>' to a '

If you wanted to handle it all in one script, you'd try this (Posting this for learning purposes):

 using UnityEngine;
 using System.Collections;
  
 public class obstacle_generation : $$anonymous$$onoBehaviour {
  
     public GameObject obstacle;
     private GameObject[] obstacles = new GameObject[10];
     private float z = -3.802516f;
     private bool obstaclesCreated = false;
  
     void Update () {
        float y = Random.Range(3.042949f, 6.350356f);
        if(obstaclesCreated == false) {
        for(int i = 0; i < obstacles.Length; i++) {
          obstacles[i] = Instantiate(obstacle, new Vector3(x * 7.5f, y, z),Quaternion.identity);
        }
        obstaclesCreated = true;
        }
  
        for(int i = 0; i < obstacles.Length; i++)
        if (obstacles[i].transform.position.x > -8) {
  
          Destroy(obstacles[i]);
  
        }
     }
 }
avatar image Jammer3000 · Feb 26, 2014 at 01:12 PM 0
Share

Thanks robertbu I caught that and forgot to say something but the deleting only the copy is something I didnt think of?! Thanks robertbu and Gambinolnd (: But Gambinolnd that script you posted gives me this error: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by GambinoInd · Feb 26, 2014 at 05:45 AM

This is because the variable obstacle is referencing the asset. What you need to do, is change

 Instantiate(obstacle, new Vector3(x * 7.5f, y, z),Quaternion.identity);

to

 obstacle = (GameObject) Instantiate(obstacle, new Vector3(x * 7.5f, y, z),Quaternion.identity);

Instead of destroying the instantiated object you were trying to destroy, you were actually attempting to destroy the original due to not assigning obstacle as the instantiated object.

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 Jammer3000 · Feb 26, 2014 at 05:51 AM 0
Share

It gives me this error when I do that: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

avatar image GambinoInd · Feb 26, 2014 at 05:52 AM 0
Share
 obstacle = (GameObject) Instantiate(obstacle, new Vector3(x * 7.5f, y, z),Quaternion.identity);

I forgot to add (GameObject) in front. You can also do this as well, they do the same thing

     obstacle = Instantiate(obstacle, new Vector3(x * 7.5f, y, z),Quaternion.identity) as GameObject;
avatar image Bonfire-Boy · Aug 08, 2019 at 01:04 PM 0
Share

Usually much better to use a new variable. Doing it this way, you've lost the prefab reference.

avatar image
2

Answer by sujitmarcus · Nov 16, 2018 at 08:01 AM

If you Don't want to Destroy Object From variable but destroy object in game Do this.

 Instantiate(particlefx, .transform.position , Quaternion.identity);
 
 Destroy(GameObject.FindGameObjectWithTag("Particle"), 1.5f);
 
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 mayankela21 · Aug 08, 2019 at 12:30 PM 0
Share

YOU ARE RIGHT mERE bHAI;

avatar image kaancetinkayasf · Mar 11, 2020 at 10:26 PM 0
Share

This one is worked for my case because I can't reach the local variable from different class

avatar image Crazy_Gamer123 · Dec 14, 2020 at 09:42 AM 0
Share

Thanks bro u helped me a lot, luv u

avatar image
1

Answer by iCodeYourMind · Dec 15, 2016 at 10:12 PM

 void Update()
 {
     if (Input.GetKeyDown (KeyCode.Space)) {
         prefabCopy = Instantiate (prefab, new Vector3 (0.0f, 1.0f, i * 1.0f), Quaternion.identity);
         Destroy (prefabCopy, 2.0f);
         i++;
     }
 }

}

I do something like this... And work perfectly...

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

27 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

Related Questions

Melle script, enemy destroyed without mouse command once in range. 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

How to toggle a key for a car to go forward or backward? 1 Answer

Scripting error #2! 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