Instantiate - Checking to see if the object already exists?

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?

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:

if(GameObject.Find ("ObjectName")){
      Debug.Log ("Exists");
}
else Debug.Log ("Doesn't exist");

@GNGification
Sorry, I didn’t specify more.
I am doing this is JS, but I think it is:

#pragma strict

var ObjectToInstantiate : GameObject;
var LocationToPlace : Vector3;

function Start () {

if (GameObject.Find("ObjectToInstantiate"))
    {
        //if it exists
    }
else
    {
       
    }

}

function Update () {

}

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.

Google the singleton pattern for Unity. It’s designed to do exactly this job.

@Kiwasi
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.

1 Like

I’m not entirely sure what you mean, but you could take a look at these:

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.

The other alternative is a preloaded scene that only ever runs once.

@GNGification
I am talking about setting the Vector3 in the inspector…

So having:

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.

1900616--122525--bandicam 2014-12-28 20-38-36-479.jpg

I have managed to achieve it with this:

#pragma strict

var ObjectToInstantiate : GameObject;
var LocationToPlace : Vector3;

function Start () {

if (GameObject.Find("ObjectToInstantiate"))
    {
        //if it exists
    }
else
    {
        Instantiate (ObjectToInstantiate, LocationToPlace, Quaternion.identity);
    }

}

function Update () {

}

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:

#pragma strict

var ObjectToInstantiate : GameObject;
var LocationToPlace : Vector3;
static var wasCreated : bool = false;

function Start () {
  if (!wasCreated))
  {
    Instantiate (ObjectToInstantiate, LocationToPlace, Quaternion.identity);
    wasCreated = true;
  }
}
1 Like

A static bool does need to be reset in OnDestroy. The singleton pattern (static reference to self) would be better.

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;
    public GameObject player;

    void Awake()
    {
        this.InstantiateController();
    }

    private void InstantiateController()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(this);
        }
        else if (this != Instance)
        {
            Debug.Log("Destroying extra GM");
            Destroy(this.gameObject);
        }
    }

    void Start()
    {
        Debug.Log("Started");
        Debug.Log("Player has not spawned. I'll make one for you.");
        Instantiate(player, new Vector2(-4, 4), Quaternion.identity);
    }
}
3 Likes