- Home /
Rigidbody2D getting destroyed in the inspector
Hello, i have struggled with this issue for about 2 days now and cant find anything on the internet about it.
This is all the parts of the script
//This variable is located in the player script
var dagger_pref : Rigidbody2D;
//This function is located in the dagger script
function OnCollisionEnter2D(col : Collision2D)
{
if(col.gameObject.tag == "Block")
{
Destroy(gameObject);
}
if(col.gameObject.tag == "Enemy")
{
Destroy(col.gameObject);
Destroy(gameObject);
}
}
The problem is that when i call for the object to be destroyed in the game, it also destroys the prefab that is attached to the playerscript which disables the player from using the daggers at all. I have tried with Destroy(this.gameObject) and Destroy(this.rigidbody2D), but they both yielded the same results. If anyone knows how to make the prefab stay in the inspector after it has been destroyed in the game i would appreciate an answer very much.
Thanks in advance.
The dagger_pref variable is located in the player script so its not in the same script as the rest of the code. The rest of the code is in a script called dagger script that is attached to the dagger prefabs.
The object which you want to destroy, how that comes in scene? By Instantiate?
The object does get destroyed, but the prefab that i have attached to a Rigidbody2D in the player script gets destroyed/deleted as well. I only want the object that actually is in the game to go away.
Answer by huntsfromshadow · Mar 12, 2014 at 08:16 PM
Okay now this makes sense.
dagger_pref = Instantiate(dagger_pref, transform.position, transform.rotation);
You are overwriting what is in dagger_pref with the result of the Insantiate. So when you destroy the object it will of course make a null.
If you want to keep the prefab around you need to make another variable.
var daggerPrefab;
var myInstance;
myInstance = Instantiage(daggerPrefab....)
Hmm okay i understand the overwrite part. And am i supposed to make the myInstance variable a gameObject or a transform? Or just leave it like you presented it?
Instantiate will return a gameobject so you want it to be that.
Okay, so a bit of fiddling with the code and a few playtests later its solved! Thank you so much for the help, i can finally continue working. I appreciate it very much. And thanks to everyone else who showed interest!
$$anonymous$$ake sure you pick an answer so the system knows this has been answered.
Answer by mrpalash · Mar 12, 2014 at 07:39 PM
Use this line instead of your Destroy function:-
Destroy(GameObject.FindWithTag("gameobject"));
Replace gameobject if your tagged name is different. Hope this makes your work... :)
Same outcome unfortunately. Im trying to figure out if i messed up big somewhere, but cant figure anything out. $$anonymous$$ight have to research for some other way to fire projectiles. Thanks anyways!
Your answer
Follow this Question
Related Questions
Destroy a GameObject 2 Answers
Destroy Prefab using Javascript 2 Answers
Check if child exists and instantiate as child 1 Answer
Destroy and Spawn an Enemy 1 Answer