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[i].name == findName)
{
if(findName == BS.nextNumber.ToString())
{
Instantiate(prefab, buttonList[i].transform.position - new Vector3 (0f, 0f, -5f), Quaternion.identity);
}
}
}
}
void Update ()
{
Start();
}
}
how can I make it so that it only spawns 1 prefab?
If you would closely look at my code u would see that without the start in the update it no longer works
Be that as it may, you should never call Start() in Update(). It's just bad style. $$anonymous$$ove it to a new method and call that method in both.
Can you explain in plain language what your script is supposed to do? I don't really understand why you check if(buttonList[i].name == findName)
and then check if(findName == BS.nextNumber.ToString())
. Is findName set externally? If not why not just check if(buttonList[i].name == BS.nextNumber.ToString())
? I ask all this because I don't think you need to necessarily check if you've already spawned the prefab to avoid doing so 20 times. You might just be able to fix the logic so it never wants to spawn it 20 times in the first place.
I am making a bingo $$anonymous$$i game and I have a bingo sheet with 24 numbers on it, each number has a button on it so that i can click on it (its on android) 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) then i can check if my random generated number matches the number that you clicked on and if so you will cross it out as you do with bingo. then i place a prefab behind the number and on each line i have a collider which checks how many prefabs it collides with if it collides with 5 objects that means you have bingo. but since it spawns 20 of them and not just 1 i cant properly check this
Answer by NoseKills · Dec 16, 2015 at 11:14 PM
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 :)
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 :D
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
}
}
}
Thank you ^^ that is exactly what I did wrong, I now called the bingo function on my buttonpress script and now it only spawns 1 like intended thank you very much for your help