Search Unity

delete instantiated objects when load new scene

Discussion in 'Multiplayer' started by RensDevolp, Sep 28, 2014.

  1. RensDevolp

    RensDevolp

    Joined:
    Aug 10, 2014
    Posts:
    31
    hi,

    i'm making a multiplayer car game using PUN networking. So here's my question: how do i delete a instantiated object when i change to singleplayer mode? i'm writhing everything in c#, exept my scene changer(in java)

    here is my network manager:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class connecttonetwork : MonoBehaviour {
    5.     SpawnSpot[] spawnSpots;
    6.  
    7.     // Use this for initializationffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    8.     void Start () {
    9.         connect ();
    10.         spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
    11.         Debug.Log("is this working?7");
    12.     }
    13.    
    14.     void connect () {
    15.                 PhotonNetwork.ConnectUsingSettings ("gewoonmijnspel0.01");
    16.         Debug.Log("is this working?6");
    17.     }
    18.        
    19.  
    20.     void OnGUI(){
    21.         GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString() );
    22.         Debug.Log("is this working?5");
    23.         }
    24.     void OnJoinedLobby() {
    25.         PhotonNetwork.JoinRandomRoom();
    26.         Debug.Log("is this working?4");
    27.         }
    28.     void OnPhotonRandomJoinFailed() {
    29.         PhotonNetwork.CreateRoom (null);
    30.         Debug.Log("is this working?3");
    31.     }
    32.     void OnJoinedRoom(){
    33.         SpawnMyPlayer ();
    34.         Debug.Log("is this working? 2");
    35.         }
    36.     void SpawnMyPlayer() {
    37.         SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length) ];
    38.         GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate ("Car", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
    39.         Debug.Log("is this working?");
    40.         ((MonoBehaviour)myPlayerGO.GetComponent("Car")).enabled = true;
    41.         myPlayerGO.transform.FindChild("Main_Camera").gameObject.SetActive(true);
    42.     }
    43. }
    And here is my Menu script:
    Code (JavaScript):
    1. ar mainMenuSceneName : String;
    2. var pauseMenuFont : Font;
    3. private var pauseEnabled = false;          
    4.  
    5. function Start(){
    6.     pauseEnabled = false;
    7.     Time.timeScale = 1;
    8.     AudioListener.volume = 1;
    9.     Screen.showCursor = false;
    10. }
    11.  
    12. function Update(){
    13.  
    14.     //check if pause button (escape key) is pressed
    15.     if(Input.GetKeyDown("escape")){
    16.    
    17.         //check if game is already paused      
    18.         if(pauseEnabled == true){
    19.             //unpause the game
    20.             pauseEnabled = false;
    21.             Time.timeScale = 1;
    22.             AudioListener.volume = 1;
    23.             Screen.showCursor = false;          
    24.         }
    25.        
    26.         //else if game isn't paused, then pause it
    27.         else if(pauseEnabled == false){
    28.             pauseEnabled = true;
    29.             AudioListener.volume = 0;
    30.             Time.timeScale = 0;
    31.             Screen.showCursor = true;
    32.         }
    33.     }
    34. }
    35.  
    36. private var showGraphicsDropDown = false;
    37.  
    38. function OnGUI(){
    39.  
    40. GUI.skin.box.font = pauseMenuFont;
    41. GUI.skin.button.font = pauseMenuFont;
    42.  
    43.     if(pauseEnabled == true){
    44.        
    45.         //Make a background box
    46.         GUI.Box(Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200), "Pause Menu");
    47.        
    48.         //Make Main Menu button
    49.         if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 - 50,250,50), "Main Menu")){
    50.             Application.LoadLevel(mainMenuSceneName);
    51.            
    52.         }
    53.        
    54.         //Make Change Graphics Quality button
    55.             if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 ,250,50), "Change Graphics Quality")){
    56.            
    57.             if(showGraphicsDropDown == false){
    58.                 showGraphicsDropDown = true;
    59.             }
    60.             else{
    61.                 showGraphicsDropDown = false;
    62.             }
    63.         }
    64.        
    65.         //Create the Graphics settings buttons, these won't show automatically, they will be called when
    66.         //the user clicks on the "Change Graphics Quality" Button, and then dissapear when they click
    67.         //on it again....
    68.         if(showGraphicsDropDown == true){
    69.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 ,250,50), "Fastest")){
    70.                 QualitySettings.currentLevel = QualityLevel.Fastest;
    71.             }
    72.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 50,250,50), "Fast")){
    73.                 QualitySettings.currentLevel = QualityLevel.Fast;
    74.             }
    75.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 100,250,50), "Simple")){
    76.                 QualitySettings.currentLevel = QualityLevel.Simple;
    77.             }
    78.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 150,250,50), "Good")){
    79.                 QualitySettings.currentLevel = QualityLevel.Good;
    80.             }
    81.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 200,250,50), "Beautiful")){
    82.                 QualitySettings.currentLevel = QualityLevel.Beautiful;
    83.             }
    84.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 250,250,50), "Fantastic")){
    85.                 QualitySettings.currentLevel = QualityLevel.Fantastic;
    86.             }
    87.            
    88.             if(Input.GetKeyDown("escape")){
    89.                 showGraphicsDropDown = false;
    90.             }
    91.         }
    92.        
    93.         //Make quit game button
    94.         if (GUI.Button (Rect (Screen.width /2 - 100,Screen.height /2 + 50,250,50), "Quit Game")){
    95.             Application.Quit();
    96.         }
    97.     }
    98. }
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    You use PhotonNetwork.Instantiate to create it. Use PhotonNetwork.Destroy to delete it.
    Or close the room (PhotonNetwork.room.open = false) and then PhotonNetwork.Disconnect().
     
  3. JamesPro

    JamesPro

    Joined:
    Mar 5, 2012
    Posts:
    509
    I use uLink but what I do is create a Root GameObject and parent all of my Instantiated Objects to the Root Object. Then before you go to load a new Zone or whatever destroy the root.
     
  4. RensDevolp

    RensDevolp

    Joined:
    Aug 10, 2014
    Posts:
    31
    oh, just figured it out. thanks tobias