- Home /
Issues with destroying a game object
Hello all, I finding that one aspect of my game is fighting me every single step of the way and any help here would be appreciated. I have a bit understanding of C# but I'm not a guru by any stretch.
So here is the issue, I am following an online tutorial to instantiate bullet, or in my case a blast the shoots out from the player. So far so good on getting the blast to spawn from the middle of the player but then comes the challenge. The blast stays where I leave it or press the space bar - forevermore radiating out. This would be great for dropping mines or decoys but that isn't my intent. The blast needs to happen, disappear, and reappear when space is pressed again etc.
I'm getting a warning saying that : "Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true); UnityEngine.Object:Destroy(Object, Single) PlayerAtk:fire() (at Assets/Scripts2019/PlayerAtk.cs:35) PlayerAtk:Update() (at Assets/Scripts2019/PlayerAtk.cs:24)"
I have also tried : Destroy(this, 2f); //it just kills the script I have also tried : Destroy(EMP2, 2f); //says I can't delete and asset I have also tried : Destroy(gameObject, 2f); //says I can't due to a null object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAtk : MonoBehaviour {
// Use this for initialization
public GameObject EMP2; //= player // Calls Emp Prefab
Vector2 blastPos; // variable for where to put he blastpos
public float fireRate = 0.5f;
float nextFire = 0.0f;
void Start (){
// perhaps find picks ups to enable limited number of blasts
}
void Update()
{
if(Input.GetKeyDown("space") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
fire ();
// when space is pressed, this will call blast wave - "void fire" and all that happens
}
}
void fire()
{
Debug.Log("blam!");
blastPos = transform.position;
blastPos += new Vector2(+0f, 0f);
Instantiate (EMP2, blastPos, Quaternion.identity);
Destroy (gameObject, 2f);
//https://www.youtube.com/watch?v=1QOsUrXWMWY
//7.30 blast creates, is solid, needs to vanish, rigid body,
// elements of what "Attack" does eg. sends rigid bodies flying,
// enemies flying in a radius around the player.
// enemies only
}
}
Thanks for your help, tips, and comments!
You should read up on how Instantiate works. Specifically
Returns
Object The instantiated clone.
Description
Clones the object original and returns the clone.
Thank you I shall have a look. The tutorial I was following didn't need to add much here.
Answer by Master_Zen · May 22, 2020 at 10:58 AM
You need to destroy the object you instantiated. For this you need to keep a refference. Just change your Fire method to somethink like this:
void fire()
{
Debug.Log("blam!");
blastPos = transform.position;
blastPos += new Vector2(+0f, 0f);
GameObject refference = Instantiate (EMP2, blastPos, Quaternion.identity);
Destroy (refference, 2f);
}
Thank you, that seems to have done the trick! Could I can see that the "refference" has allowed it to work correctly but why is this the case when the tutorial I followed didn't have to do this?
void fire()
{
Debug.Log("blam!");
blastPos = transform.position;
blastPos += new Vector2(+0f, 0f);
//Instantiate (E$$anonymous$$P2, blastPos, Quaternion.identity);
//Destroy (gameObject, 2f);
GameObject refference = Instantiate (E$$anonymous$$P2, blastPos, Quaternion.identity);
Destroy (refference, 0.3f);
}
You surely have made some changes because the code of the fire method itself should not work, BUT if you just look at this line: Destroy (gameObject, 2f);
this can work if the script that contains this line sits on the bullet (or the gameobject that needs to destroyed again). The gameObject
is a pointer to the real Object sitting in your scene, after you Instantiated it. It comes from $$anonymous$$onoBehavior the class all GameObjects inherit from.
Ah, I wonder if I had attached this the 'blast' and not the player it may have been different. Thinking back to it. Thank you for your help!
And when i say it should not work, i mean not in the intended way. You set the position of the bullet to transform.position
, which is the position of the object the script is attached to. And if you call this method on a bullet, then it will always spawn at the same position. To be exact, the position the prefab has.
Answer by GoGoLoN · May 22, 2020 at 11:03 AM
Do you want to destroy the object you instantiate or the one that the script is attached to? I guess this is what you want to do:
GameObject blastObj = Instantiate (EMP2,blastPos, Quaternion.identity) as GameObject;
Destroy(blastObj, 2f);