- Home /
Stop Object from deleting HELP ME!!!!!!!!!!!!
Hey guys i just noticed something i made a script earlier on which enables and disables my scripts when the right key is pressed it enables it or disables it. I will give an example When i press the N key my current player will get deleted from the prefab that it came from and the new object will show up, and at the end it will stop the move script for the first player. Now the problem is when i press the M key it should bring back the main Player that i was using but it brings up nothing it deletes the second player and then does not transform the main player back this is because it has deleted my Player from the prefab i was wondering if i could turn its Check Box of instead of deleting the player here is my script:
//drag your spawn point to here in the inspector - probably make it an empty gameobject so you can move it about on the diving board
var spawnPlayer1 : Transform;
var MainPlayerPlayer2: GameObject;
var NewPlayerPrefab2 : Transform;
function Update()
{
if(Input.GetKey(KeyCode.M)){
Destroy(MainPlayerPlayer2); // this is the main line, this deletes the gameobject can i disable it instead?
Instantiate(NewPlayerPrefab2,spawnPlayer1.position,spawnPlayer1.rotation);
GetComponent(NewMoveScript).enabled = true;
} }
Answer by TheDemiurge · Nov 02, 2010 at 06:17 AM
What Destroy does is to remove an object from the scene, or to remove a component from an object. I would use SetActiveResursively. This is the same as clicking that little checkbox at the top-left corner of the Inspector. The main difference with this and GameObject.active
is that this one also turns any child objects on and off at the same time. This way when you turn the first player back on, whatever information they had - position, direction, speed (and higher level stuff like current weapon, ammo, gold, health, etc) are all as they were.
Thank you very much :) But now i am getting a different problem it now makes duplicates of the player and activating the script no longer works the player shows up perfectly :)
Did you delete the line with the Instantiate
? That's likely what's duplicating the player. As for activating the script, components (such as $$anonymous$$onoBehaviour) that are enabled, will automatically disable when you disable the GameObject they're attached to, and automatically enable when you reactivate that GameObject. So you shouldn't technically have to enable it yourself.
If you need to do certain things with each player just before, or just after it gets turned on or off, you can use the OnEnable
and OnDisable
functions.
Sorry about late reply, thing doesn't email on comments...