Search Unity

Need help C# x.x

Discussion in 'Scripting' started by Deleted User, Nov 22, 2014.

  1. Deleted User

    Deleted User

    Guest

    I made a 2d platformer game where there are traps for example disappearing floor. Once I die and respawn as a clone the scene doesnt get reseted.. If I try Application.Loadlevel(); it restarts everything and I can't add check points... This is the code. Also while standing on a slope makes my character slide back down it doesnt just stand there

    Code (CSharp):
    1. void  OnGUI (){
    2.      
    3.         if (text){
    4.             GUI.Label ( new Rect(Screen.width / 2.9f, Screen.height / 8, 444,138), YouDied);
    5.             GUI.backgroundColor = Color.clear;
    6.             bool menu= GUI.Button ( new Rect(Screen.width / 1.3f, Screen.height - (Screen.height * 0.9f / 4.3f), 185,67), MainMenu);
    7.             bool retry= GUI.Button ( new Rect(Screen.width / 2.3f, Screen.height - (Screen.height * 0.9f / 4.3f), 185,67), pressSpacebar);
    8.             GUI.Label ( new Rect(Screen.width / 2.13f, Screen.height - (Screen.height * 0.9f / 3.2f), 185,47), Retry);
    9.             if (menu){
    10.                 Application.LoadLevel(0);
    11.                 score = 0;
    12.             }
    13.             else if (Input.GetKey("space")){
    14.                 text = false;
    15.                 var P = (GameObject)Instantiate(Player, spawnPoint.position, Quaternion.identity);
    16.                 P.name = "Player";
    17.                 var sf = Camera.main.GetComponent<CameraFollow>();
    18.                 sf.player = P.transform;
    19.                 Dea.enabled = false;
    20.                 ths.enabled = false;
    21.                 number.enabled = false;
    22.             }
    23.         }
    24.     }
    please help :D
     

    Attached Files:

    Last edited by a moderator: Nov 22, 2014
  2. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    Not sure if I understand it correct, but why not showing the "final" screen in a different leve/scene with a button to restart the game (and reload the scene)?
     
  3. Deleted User

    Deleted User

    Guest

    If I restart (makes a clone instead of restarting the scene) it just makes a clone where my spawnpoint is at but I want the scene to reset when I respawn. Basically want to do to Application.Loadlevel but instead loads at all my spawnpoints
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824

    You could make a static vector3 variable and always assign the latest (re)spawn point to it. When you die, reload the level and make your character spawn at that position rather than a hardcoded starting position.
    As it's static, it should always keep the value until you quit the game (or change it, of course).
     
  5. Deleted User

    Deleted User

    Guest

    How would I do that?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameOver : MonoBehaviour {
    5.    
    6.    
    7.     public bool text = false;
    8.     public Texture YouDied;
    9.     public Texture MainMenu;
    10.     public Texture pressSpacebar;
    11.     public Texture Retry;
    12.     public GameObject Player;
    13.     public Transform spawnPoint;
    14.  
    15.     public bool text2 = false;
    16.     public static int score = 0;
    17.     public static int currentScore = 0;
    18.     public GUIText Deaths;
    19.     public GUIText TryAgain;
    20.     public GUIText Menu;
    21.     public GUIText PressSpacebar;
    22.     public GUIText MenuHover;
    23.  
    24.     private CameraFollow cameraFollow;
    25.     private Menu2 OnHover;
    26.  
    27.     void  OnTriggerStay2D(Collider2D other){
    28.         if(other.tag == "Player"){
    29.             score += 1;
    30.             Deaths.text = "<i><color=red>Dea</color><color=teal>ths:</color></i> " +score;
    31.             Deaths.enabled = true;
    32.             Menu.enabled = true;
    33.             TryAgain.enabled = true;
    34.             PressSpacebar.enabled = true;
    35.             MenuHover.enabled = true;
    36.             text2 = true;
    37.         }
    38.  
    39.         if (gameObject.name == "Spikes"){
    40.             GetComponent<SpriteRenderer>().enabled = true;
    41.         }
    42.         text = true;
    43.         Destroy(other.gameObject);
    44.     }
    45.     void  OnGUI (){
    46.        
    47.         if (text){
    48.             GUI.Label ( new Rect(Screen.width / 2.9f, Screen.height / 8, 444,138), YouDied);
    49.  
    50.          if (Input.GetKey("space")){
    51.                 text = false;
    52.                 Application.LoadLevel(Application.loadedLevel);
    53.                 Deaths.enabled = false;
    54.                 Menu.enabled = false;
    55.                 TryAgain.enabled = false;
    56.                 PressSpacebar.enabled = false;
    57.                 MenuHover.enabled = false;
    58.             }
    59.         }
    60.     }
    61.     void Update (){
    62.         if(score < currentScore){
    63.             currentScore = score;
    64.         }
    65.         else{
    66.             currentScore = score;
    67.         }
    68.     }
    69. }
    This is my code when the player dies
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well, for me it wouldn't be that class to handle the spawning, but you got a spawnPoint of type Transform there.
    I'd say choose Vector3 for it and make it static, something like:

    Code (CSharp):
    1. public static Vector3 spawnPoint = new Vector3(yourX, yourY, yourZ);
    The assignment is just the initial/default value representing your very first spawnPoint, of course you have to pass valid arguments.

    Whenever you reach a checkpoint or a place to respawn in your game, you'll re-assign your variable with that position of the new spawnPoint.

    Your player should also be "spawned" via script in this case, no need to actually instantiate it with code, but at least set its position via script to GameOver.spawnPoint (note that GameOver.spawnPoint will only work if you make spawnPoint static).
     
  7. Deleted User

    Deleted User

    Guest

    oh ok thank you very much :D