- Home /
Deleting a clone when the clone is being made on another sprite
I am making A tank game when the player clicks r the game will spawn the tanks and then they battle and they can shoot bullets at each other but when the bullet hits the enemy tank it is deleting the bullet clone like it is supposed to but it is not deleting the tank clone and is coming up with the error message "Destroying assets is not permitted to avoid data loss".
Answer by thirtiesareoldies · Jun 09, 2020 at 02:58 AM
Yeah so I think your code is trying to delete the prefab itself which is a problem. Instead, try saying Object.Destroy(gameObject); Object.Destroy(coll.gameObject);
@thirtiesareoldies Hi Thanks for the answer but it is just saying that Collision2D' does not contain a definition for 'Tank_Hit if you know how to fix this it would be great thanks.
Answer by Soxbear · Jun 09, 2020 at 02:49 PM
I'm new, so I might be wrong, but, @Jsmithson ,
What you are doing here is destroying an object in your projectiles script, and it appears that you are trying to destroy an object referenced in a public variable, which you would most likely set to the prefab I would guess. What you want to do is have a collision detection in the tank using the Update() function, which will have the tank destroy itself, and then the bullet detects if it hit a tank in the LateUpdate() function, meaning they will both be destroyed in the same frame. Keep in mind I am new, but I have just gone through a problem with the destruction of objects in a shooter, so I think I know what I am saying.
@Soxbear How would I make the tank destroy itself as I am cloning it in the $$anonymous$$ain Camera as it is the first thing to spawn.
@Jsmithson you would attach a script the to the one object that holds everything for the tank on the prefab, and that script should hold the health script and bullet detection. When health equals 0, you use the Destroy(); function
Also in case you didn't see the update it does require a rigidbody, but you can set it to is kinematic, or use a character controller
Answer by someonethatis · Jun 09, 2020 at 09:06 PM
Are you refrenceing a prefab? you can use a trigger colider on the bullet so it will kill itself when touched by a bullet. you can use a onTriggerEnter if your projectile has a trigger colider or you can add one by using a mesh collider or a collider that is the right shape.
@someonethatis The bullet deletes fine it is just destroying the tank clone that I need help with.
Are you destroying a component like collider for trigger or transform for raycast
if so just use .gameObject if that doesn't work just try .transform.gameObject
Answer by RealEfrain12 · Dec 12, 2020 at 11:00 AM
You could try this, if your prefab tanks don't already have a collider2D on it, add it, then make sure ypur tank prefabs have a tag of "Tank", then add a script (Make sure to add this script to the bullet prefab) and type this:
void OnCollisionEnter2D(Collision2D collision)
{
switch(collision.gameObject.tag)
case "Tank":
Destroy(collision.gameObject); //Destroys the bullet
Destroy(gameObject); //Destroys the tank
break;
}
Note: I added a switch statement, just in case you wanted the bullet to attack more than one thing with a tag, if you only plan on adding a single function to the bullet which is destroy the bullet and tag, you can just type:
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Tank")
{
Destroy(collision.gameObject);
Destroy(gameObject);
}
}
Your answer

Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How in the world do I ACCURATELY place an object at the mouse? 3 Answers
Increase force/size the longer input has been held down. 2 Answers
Offseting insantiated object 1 Answer