Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Singleton Keep Original Copy.

Discussion in 'Scripting' started by guyanermanator, Feb 19, 2020.

  1. guyanermanator

    guyanermanator

    Joined:
    Mar 17, 2018
    Posts:
    13
    So I am trying to keep an Object Persistent but I want to only keep the original object and discard the Duplicate. Sorry for some mistakes in my code. The Copies of this object in my game always come out weird so I really prefer to keep the original object. Here is my code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class Singleton : MonoBehaviour
    7. {
    8.     private static Singleton _instance;
    9.     public  static Singleton Myself;
    10.     public GameObject Dupe;
    11.     public int MyID;
    12.    
    13.     public static Singleton Instance
    14.     {              
    15.         get { return _instance; }
    16.     }
    17.  
    18.     private void Awake()
    19.     {
    20.         MyID = -98560;
    21.         Myself = gameObject.GetComponent<Singleton>();
    22.  
    23.         if (Myself.GetInstanceID() != MyID)
    24.         {
    25.             Dupe = this.gameObject;
    26.             Destroy(Dupe);
    27.  
    28.             Debug.Log("Fake");
    29.  
    30.         }
    31.         else if (Myself.GetInstanceID() == MyID)
    32.         {
    33.  
    34.             DontDestroyOnLoad(Myself);
    35.             Debug.Log("Originoal");
    36.         }
    37.  
    38.  
    39.     }
    40.  
    41.      void Update()
    42.     {
    43.      
    44.     }
    45.  
    46.  
    47. }
    48.  

    or if there is a way to retain Object IDs or Dynamically Pull and differentiate between the old and new one. Thanks for your time.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Instance ids are not persistent between runs. You should not rely on that.
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    a)
    Code (csharp):
    1. Myself = gameObject.GetComponent<Singleton>();
    this could just be
    Code (csharp):
    1. Myself = this
    ;

    b) Just check if your Instance isn't null already
    Code (csharp):
    1.  
    2. //at beginning of awake
    3. if(Instance != null)
    4. {
    5.     Destroy(this.gameObject);
    6.     return;
    7. }
    8.  
    9. //the rest of awake
    10. Instance = this;
    11.  
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    In the Editor, this will set Instance to the instance what was in currently active scene. AFAIK, the OP wants the instance from his starting scene. For this, you need to add check for gameObject.scene.buildIndex == 0.