Instantiate prefab if no prefab exists at location

I have a little script which spawns a prefab at a location I want, but since it is done in a for loop while something is true (it only stays true for a bit) I want 1 prefab to spawn but instead if spawns like 20 until the condition is no longer true,
This is my code:

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

public class DetectScript : MonoBehaviour 
{
	public List<GameObject> buttonList = new List<GameObject>();
	public string findName;
	public BingoScript BS;
	public GameObject prefab;

	void Start()
	{

		for (int i = 0; i < buttonList.Count ; i++)
		{
			if(buttonList*.name == findName)*
  •  	{*
    
  •  		if(findName == BS.nextNumber.ToString())*
    
  •  		{*
    

_ Instantiate(prefab, buttonList*.transform.position - new Vector3 (0f, 0f, -5f), Quaternion.identity);_
_
}_
_
}_
_
}_
_
}*_

* void Update ()*
* {*
* Start();*
* }*
}
how can I make it so that it only spawns 1 prefab?

Of course i can’t be sure without knowing more about how the game works, but it feels strange that if this is your “check for correct answer” routine, you are doing it all the time in Update().

There’s no need to constantly keep checking the answer if the input that could cause the puzzle to be solved only changes every now and then (when pressing the buttons?). You should check the solution only when those buttons are pressed. And for clarity’s sake you should put the checking code into a method called CheckSolution() or something like that, and call that method in Start() and other methods instead of calling Start() in Update().

All this being said, you are doing things a bit funky overall. I do commend you on your work and for using Unity Answers exactly as supposed to - doing your own coding and asking for help with a proper code sample - but I’ll still give you a few pointers. Don’t be disencouraged by this :slight_smile:

I think it’s very helpful to keep your game logic and UI/Graphics as separated from each other as possible. Now you are using GameObjects and colliders (physical, spatial, graphical objects) to check for a bingo result based on the location and collision of objects, when in fact Bingo is only numbers: a list of numbers that the player chooses and a list of numbers that the game generates - if those lists match, it’s a bingo.

when you click on it the findName is then set to the name of the button (which is named after the number that is on it)

So you already change a variable when a button is pressed. Now all you’d need to do is to instead of only changing a variable upon button press, call a method that both takes the variable and checks for the answer after that once. I’d probably do it something like this. EDIT: I forgot how bingo is played but you get the idea :smiley:

using UnityEngine;
using System.Collections.Generic;

public class Bingo : MonoBehaviour {

	public List<int> PlayersNumbers = new List<int>();
	public List<int> BingoNumbers = new List<int>();

	bool IsBingo() {
		foreach (var bingoNumber in BingoNumbers) {
			if (!PlayersNumbers.Contains(bingoNumber)) {
                    // players numbers didn't have this bingo number in them, can't be bingo
				return false; 
			}
		}
        // all bingo numbers were found in the list of player picked numbers
		return true;
	}
	// called from your button
	public void NumberCliked(int number) {
		if (!PlayersNumbers.Contains(number)) {
			PlayersNumbers.Add(number);
		}

		if (IsBingo()) {
			//Do a victory dance
		}
	}
}