Search Unity

C# DontDestroyOnLoad

Discussion in 'Scripting' started by Nefarem, Jul 5, 2015.

  1. Nefarem

    Nefarem

    Joined:
    Jul 3, 2015
    Posts:
    6
    Hi,

    I have a problem with loading the new scene , when it is done I am everywhere, deleted GameObject.
    I read the script that you need to add " DontDestroyOnLoad ", I said it but it still did not help but interestingly appeared on the list MainCamera



    Code:


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Test2 : MonoBehaviour {
    6.  
    7.    static Test2 Instance;
    8.  
    9.  
    10.    void Start()
    11.    {
    12.      if(Instance != null)
    13.      {
    14.        GameObject.Destroy(gameObject);
    15.      }
    16.      else
    17.      {
    18.        GameObject.DontDestroyOnLoad(gameObject);
    19.        Instance = this;
    20.  
    21.      }
    22.  
    23.    }
    24.  
    25.    void Update()
    26.    {
    27.  
    28.    }
    29.  
     
  2. Nefarem

    Nefarem

    Joined:
    Jul 3, 2015
    Posts:
    6
    refresh
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I'm sorry but it's not entirely clear what it is you are trying to do... could you explain what it is you are trying to achieve?
     
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Gross...

    Please watch my tutorial on Singleton Design.

    Also, please include more detail as to what you're trying to achieve.
     
  5. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    Is "Test2" on all of those "deleted gameobject"s, or is it only on Main Camera? I've seen that error crop-up before but I forget what it is how or how I solved it. Are you destroying anything in the editor? Any script have ExecuteInEditMode on it?
     
  6. Nefarem

    Nefarem

    Joined:
    Jul 3, 2015
    Posts:
    6

    I have a 2D game and I have done all the 1 and 2 level and at the end of the 1st mission is to " Application.LoadLevel ( " Level02 ")"
    and when it reads a 2- level everything is suddenly removing and this is jamming .

    And he wants to level 2 has loaded without a problem and was fully playable .
     
  7. Nefarem

    Nefarem

    Joined:
    Jul 3, 2015
    Posts:
    6
    refresh ;/