Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiate - Checking to see if the object already exists?

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Dec 28, 2014.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am attempting to instantiate my player character controller to prevent duplication on Don'tDestroyOnLoad, this is something I cannot do without unfortunately due to the way we have designed the maps.
    So to prevent duplication, we want to instantiate the character ONLY if one doesn't already exist in the scene we are loading, so it would most likely only do it once per new game started, but I think personally that this would be the best way to go.

    Can anybody help us with this?
     
  2. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    Couldn't you just create an if-statement and search the scene to see if a game object with that name or tag already exists? Like something as simple as this:

    Code (CSharp):
    1.  
    2. if(GameObject.Find ("ObjectName")){
    3.       Debug.Log ("Exists");
    4. }
    5. else Debug.Log ("Doesn't exist");
    6.  
     
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    @GNGification
    Sorry, I didn't specify more.
    I am doing this is JS, but I think it is:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var ObjectToInstantiate : GameObject;
    4. var LocationToPlace : Vector3;
    5.  
    6. function Start () {
    7.  
    8. if (GameObject.Find("ObjectToInstantiate"))
    9.     {
    10.         //if it exists
    11.     }
    12. else
    13.     {
    14.        
    15.     }
    16.  
    17. }
    18.  
    19. function Update () {
    20.  
    21. }
    My problem is working out how to go from there, how to instantiate using a Vector given as a variable, we can't find anything online that works.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Google the singleton pattern for Unity. It's designed to do exactly this job.
     
  5. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    @BoredMormon
    I am having quite a bit of trouble working out exactly what a Singleton is, with the code I found, they don't seem to do anything at all, they seem quite useless, although it may just be what I found, but I think the way I mentioned would be the easiest way for us to do it.
     
    lyingturkey likes this.
  6. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    I'm not entirely sure what you mean, but you could take a look at these:
    http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    In plain English the singleton runs like this
    • Have a static variable of the type of your class
    • In awake check if the value of your static is null. If it is not null destroy the game object
    • Otherwise assign the current I dance to the static variable and continue as normal.
    This pattern guarantees that only one version of the script can exist at any time.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The other alternative is a preloaded scene that only ever runs once.
     
  9. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    @GNGification
    I am talking about setting the Vector3 in the inspector...

    So having:

    Code (JavaScript):
    1. var LocationToPlace : Vector3;
    Would give me the ability to set the 3 vectors (X, Y, Z) in the inspector window with the script, just underneath the box for the ObjectToInstantiate GameObject.
     

    Attached Files:

  10. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I have managed to achieve it with this:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var ObjectToInstantiate : GameObject;
    4. var LocationToPlace : Vector3;
    5.  
    6. function Start () {
    7.  
    8. if (GameObject.Find("ObjectToInstantiate"))
    9.     {
    10.         //if it exists
    11.     }
    12. else
    13.     {
    14.         Instantiate (ObjectToInstantiate, LocationToPlace, Quaternion.identity);
    15.     }
    16.  
    17. }
    18.  
    19. function Update () {
    20.  
    21. }
     
  11. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    You're unnecessarily slowing things down with that Find call, which also feels yucky to me because you're relying on the object's name.

    It's much simpler to use a static bool:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var ObjectToInstantiate : GameObject;
    4. var LocationToPlace : Vector3;
    5. static var wasCreated : bool = false;
    6.  
    7. function Start () {
    8.   if (!wasCreated))
    9.   {
    10.     Instantiate (ObjectToInstantiate, LocationToPlace, Quaternion.identity);
    11.     wasCreated = true;
    12.   }
    13. }
     
    markasuter likes this.
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    A static bool does need to be reset in OnDestroy. The singleton pattern (static reference to self) would be better.
     
  13. markasuter

    markasuter

    Joined:
    May 20, 2019
    Posts:
    22
    For those visiting in 2020+, here's how I solved this (Yes, singletons are probably better...but I got confused by them).

    It's a GameManager script that spawns the player. If one already exists, destroy the extra one. You can see the entire Unity github repo here. To test, open the B2 scene in the "RoomSwitching" folder and run.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     public static GameManager Instance;
    8.     public GameObject player;
    9.  
    10.     void Awake()
    11.     {
    12.         this.InstantiateController();
    13.     }
    14.  
    15.     private void InstantiateController()
    16.     {
    17.         if (Instance == null)
    18.         {
    19.             Instance = this;
    20.             DontDestroyOnLoad(this);
    21.         }
    22.         else if (this != Instance)
    23.         {
    24.             Debug.Log("Destroying extra GM");
    25.             Destroy(this.gameObject);
    26.         }
    27.     }
    28.  
    29.     void Start()
    30.     {
    31.         Debug.Log("Started");
    32.         Debug.Log("Player has not spawned. I'll make one for you.");
    33.         Instantiate(player, new Vector2(-4, 4), Quaternion.identity);
    34.     }
    35. }