- Home /
removing an instantiated prefab
I am close to finishing this step. far too many step left to count but that is another issue. I have been able to add a knife to my player however, I have not found the way to remove it. I will show a code snippet.
var up:Transform;
var dn:Transform;
var knife:Transform;
private var weapon:Transform = null;
function FixedUpdate() {
if ( Input.GetKeyDown(KeyCode.Alpha0) ) {
SetMode(0);
Destroy(knife); // have also tried Destroy(weapon);
weapon = null;
}
else if ( Input.GetKeyDown(KeyCode.Alpha9) ) {
SetMode(3);
if ( weapon == null ) {
weapon = new Instantiate(knife, Vector3(0, 0, 0), Quaternion.identity);
weapon.transform.parent = dn.transform;
weapon.transform.localPosition = Vector3(0, 0, 0);
weapon.transform.localRotation = dn.localRotation;
}
} else if ( Input.GetKeyDown(KeyCode.Alpha8) ) {
SetMode(4);
if ( weapon == null ) {
weapon = new Instantiate(knife, Vector3(0, 0, 0), Quaternion.identity);
weapon.transform.parent = up.transform;
weapon.transform.localPosition = Vector3(0, 0, 0);
weapon.transform.localRotation = up.localRotation;
}
}
}
the weapon appears as expected when I press 8 or 9 but when I press 0 the script errors. the error when Destroy(knife); is:
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) PlayerAnimation:FixedUpdate() (at Assets/manikin/PlayerAnimation.js:44)
that would destroy the prefab from the project right?
the error for Destroy(weapon); is:
Can't destroy Transform component. If you want to destroy the game object please call 'Destroy' on the game object instead. Destroying the transform component is not allowed.
I think I am close but I don't see anything in the unity doc that helps, though it is big so I could have missed it. Does any know the problem?
oh, and I tried making both knife and weapon GameObject types howevr, I get this error:
InvalidCastException: Cannot cast from source type to destination type. PlayerAnimation.FixedUpdate () (at Assets/manikin/PlayerAnimation.js:58)
Answer by whydoidoit · Aug 22, 2013 at 06:22 AM
You certainly don't want to Destroy knife as that's the prefab - what you actually need is to destroy the GameObject of the instance you've made in weapon:
Destroy(weapon.gameObject);
Thank you for your quick reply however, it produced this error:
NullReferenceException: Object reference not set to an instance of an object PlayerAnimation.FixedUpdate () (at Assets/manikin/PlayerAnimation.js:45)
do I need something like?:
weapon.AddComponent = new Instantiate(knife, Vector3(0, 0, 0), Quaternion.identity);
No you don't need that (and indeed it would not compile).
Your code should be in Update not FixedUpdate - because FixedUpdate is not for Input and is for physics. FixedUpdate can run multiple times per frame and so your code to destroy things could run many times in a frame.
I also suggest you do
if(weapon != null) Destroy(weapon.gameObject).
Sweet works like a charm, I'll move that code to the regular update. Thank you so much!!!
I tried to use that code and it didn't work :/
I am calling the function from within the void Update. I include the code below and would seriously appreciate any help! I have a feeling I am close... just not close enough!
void Update ()
{
IsFailed (); //Check constantly if the player is winning
Play (); //Launch the level on click
loopDone = false;
PausePlay();
}
void PausePlay()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P)) //p button as pause input
{
if (Time.timeScale == 1)
{
Time.timeScale = 0; //pause that bitch and show the tagged PauseScreen objects
showPaused();
}
else if (Time.timeScale == 0)
{
Time.timeScale = 1;
hidePaused(); //hide showonpause objects
}
}
}
public void showPaused() //shows objects with ShowOnPause tag - when in canvas
{
GameObject prefab = Instantiate(Resources.Load<GameObject>("Pause$$anonymous$$enu")); //Load prefab }
public void hidePaused() //hides objects with ShowOnPause tag
{
Destroy(prefab.gameObject); //also tried with Destroy(Pause$$anonymous$$enu.gameObject);
}