- Home /
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);
}
}
}
@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]);
}
}
}
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?)
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.
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?)
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;
Usually much better to use a new variable. Doing it this way, you've lost the prefab reference.
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);
This one is worked for my case because I can't reach the local variable from different class
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...