Only one of the same game objects.

Hello, I have a 3d game and in it, I only want one of the same game objects. Is there any way to check if there is more than one sphere? And then the extra spheres will be destroyed, can someone guide me in the right direction? Thanks! (The game object is called sphere (clone) )

You have to write a script because there is no such behavior built-in Unity.

You need some basic knowledges in programming and must understand the concept of classes. When you’re ready, you can create a class to detect unicity (this is called a Singleton), when an instance of this class is instantiated, you check if there are any other instances of this class already existing. You can then either the former one or the new one.

class UniqueSphere : MonoBehaviour
{
    private void Start()
    {
        var other = FindObjectOfType<UniqueSphere>();

        if (other != null)
        {
             Destroy (gameObject);
        }
    }
}

As Mouton suggested, you should use a Singleton to accomplish this.

Something like the following would immediately destroy any object with the component attached if another object with the component already exists. (So this will be most effective when using Prefabs)

[DisallowMultipleComponent]
public class Singleton : MonoBehaviour {
    private static Singleton _instance;
    public static Singleton Instance {
        get { return _instance; }
        set {
            if (_instance == null) {
                _instance = value;
            } else {
                Destroy(value.gameObject);
            }
        }
    }

    private void Awake () {
        Instance = this;
    }
}

Using some sort of ‘object pooler’ might help you.

You write a script having List<List<GameObject>> pools (or use a list of structs including a List<GameObject> and a string Name for easy comparing).
When you want to spawn an object you do so through this class that always takes the existing object and ‘resets’ is values so u can use it as a new object.

Make sure every game object in your pool has a “reset” function to reset all their value’s and act like a new object.

p.s. you want pools of one so doing List<GameObjects> should also work but adding the second list makes it usable in other projects where u use bigger pools.

Thanks for the reply Kishotta but when I clone the clone it deletes the clone instead of the first game object, can I make it so it deletes the oldest gameobject? Thanks!

Thanks again for all the answers but sorry for telling you to write that code because it will delete things so the player can see random stuff deleted. I am using kishottas first code and attaching a destroy in 7 seconds so when the sphere is done with its actions it will get deleted. That works but when I clone my sphere I receive The object of type ‘GameObject’ has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. Also, this is my cloning script (kindly provided by xxmariofer) `using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Clone : MonoBehaviour
{
public GameObject prefab;
public float x=2;//i dont know what x is
void Start() { StartCoroutine(Cloner()); }
float frequence = 10;
float timer = 0;
void Update()
{
if (timer > 10)
{
timer = 0; frequence = frequence / 1.5f;
}
timer += Time.deltaTime;
}

IEnumerator Cloner()
{
    while (true)
    {
        yield return new WaitForSeconds(frequence);
        Instantiate(prefab, new Vector3(Random.Range(0, 3), 0, Random.Range(0, 3)), Quaternion.identity);

   

    }
}

}
` Thanks!

Thanks for all the answers! You all deserve a like =). For anyone who is viewing this in the future all of the answers work, but if you do not want to change anything and just want copy-paste the script go to Kishotta but some other answers might be better in your case. And thanks again =).