Search Unity

Respawning Enemy WITH the Player?

Discussion in 'Scripting' started by Quist, Mar 5, 2014.

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    277
    So i have made a script which makes my player get a "Respawn" option when his Health hits 0.

    But when i respawn my player the enemies the player killed are killed and they stand where they were when he died.

    What do i have to add to this code to make that work?

    Code (csharp):
    1.  #pragma strict
    2.  
    3. var lookAround01 : MouseLook;
    4. var lookAround02 : MouseLook;
    5. var charController : CharacterController;
    6.  
    7. var respawnTransform : Transform;
    8.  
    9. static var playerIsDead = false;
    10. static var enemyIsDead = false;
    11. function Start ()
    12. {
    13.     lookAround01 = gameObject.GetComponent(MouseLook);
    14.     lookAround02 = GameObject.Find("MainCamera").GetComponent(MouseLook);
    15.     charController = gameObject.GetComponent(CharacterController);
    16. }
    17.  
    18. function Update ()
    19. {
    20.     if (playerIsDead == true)
    21.     {
    22.         lookAround01.enabled = false;
    23.         lookAround02.enabled = false;
    24.         charController.enabled = false;
    25.     }
    26. }
    27.  
    28. function OnGUI ()
    29. {
    30.     if (playerIsDead == true)
    31.     {
    32.         if (GUI.Button(Rect(Screen.width*0.5-50, 200-20, 100, 40),"Respawn"))
    33.         {
    34.             RespawnPlayer();
    35.         }
    36.         if (GUI.Button(Rect(Screen.width*0.5-50, 240, 100, 40),"Menu"))
    37.         {
    38.             Debug.Log("Return to Menu");
    39.         }
    40.     }
    41. }
    42.  
    43. function RespawnPlayer()
    44. {
    45.     transform.position = respawnTransform.position;
    46.     transform.rotation = respawnTransform.rotation;
    47.     gameObject.SendMessage("RespawnStats");
    48.     lookAround01.enabled = true;
    49.     lookAround02.enabled = true;
    50.     charController.enabled = true;
    51.     playerIsDead = false;
    52.     Debug.Log("player has respawned");
    53. }


    i tried the following but it didn´t work "the text in Italic is the new stuff i added" :



    Code (csharp):
    1.  #pragma strict
    2.  
    3. var lookAround01 : MouseLook;
    4. var lookAround02 : MouseLook;
    5. var charController : CharacterController;
    6.  
    7. var respawnTransform : Transform;
    8.  
    9. static var playerIsDead = false;
    10. [I]static var enemyIsDead = false;[/I]
    11.  
    12. function Start ()
    13. {
    14.     lookAround01 = gameObject.GetComponent(MouseLook);
    15.     lookAround02 = GameObject.Find("MainCamera").GetComponent(MouseLook);
    16.     charController = gameObject.GetComponent(CharacterController);
    17. }
    18.  
    19. function Update ()
    20. {
    21.     if (playerIsDead == true)
    22.     {
    23.         lookAround01.enabled = false;
    24.         lookAround02.enabled = false;
    25.         charController.enabled = false;
    26.     }
    27. }
    28.  
    29. function OnGUI ()
    30. {
    31.     if (playerIsDead == true)
    32.     {
    33.         if (GUI.Button(Rect(Screen.width*0.5-50, 200-20, 100, 40),"Respawn"))
    34.         {
    35.             RespawnPlayer();
    36.         }
    37. [I]     {
    38.             RespawnEnemy();
    39.         }[/I]
    40.         if (GUI.Button(Rect(Screen.width*0.5-50, 240, 100, 40),"Menu"))
    41.         {
    42.             Debug.Log("Return to Menu");
    43.         }
    44.     }
    45. }
    46.  
    47. function RespawnPlayer()
    48. {
    49.     transform.position = respawnTransform.position;
    50.     transform.rotation = respawnTransform.rotation;
    51.     gameObject.SendMessage("RespawnStats");
    52.     lookAround01.enabled = true;
    53.     lookAround02.enabled = true;
    54.     charController.enabled = true;
    55.     playerIsDead = false;
    56.     Debug.Log("player has respawned");
    57. }
    58.  
    59. [I]function RespawnEnemy
    60. {
    61.     transform.position = respawnTransform.position;
    62.     transform.rotation = respawnTransform.rotation;
    63.     gameObject.SendMessage("RespawnStats");
    64.     enemyIsDead = false;
    65.     Debug.Log("enemy has respawned");[/I]
    66. }
    Any ideas what i can do?
     
  2. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    277
    The stuff inside the Brackets is what i made in the new one which didn´t work either! :O
     
  3. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    120
    I'm not sure I completely understand the issue you are having but a model that I am particularly fond of is to have a script with a Reset function on every object that would need to reset itself during game play (this could also be called during initialization) and when the player dies broadcast the reset function to every object so it will have the opportunity to react.

    http://docs.unity3d.com/Documentation/ScriptReference/GameObject.BroadcastMessage.html
     
  4. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    277
    precisely, i want it to happen so when my charactre dies, it should reset EVERYTHING to it´s starter settings, such as where the enemy object is at the start.
     
  5. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    120
    I think you sort of missed my point :).

    Take a look at that function in the link I gave and (probably better yet for this specific case) BroadcastMessage to set up a system where this will work easily and you'll be able to define new reset functionality in new objects without needing to keep track in some centralized location of what objects need to reset.
     
unityunity