Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by cadeautje · Dec 14, 2015 at 02:17 PM · c#instantiateprefabposition

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?

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Positive7 · Dec 14, 2015 at 03:40 PM 1
Share

Remove Start(); from Update function

avatar image cadeautje Positive7 · Dec 15, 2015 at 08:21 AM 0
Share

If you would closely look at my code u would see that without the start in the update it no longer works

avatar image ZefanS cadeautje · Dec 15, 2015 at 08:47 AM 0
Share

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.

Show more comments
avatar image ZefanS · Dec 16, 2015 at 12:54 AM 0
Share

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.

avatar image cadeautje ZefanS · Dec 16, 2015 at 10:14 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cadeautje · Dec 17, 2015 at 11:50 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

45 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate not spawning at correct position 1 Answer

Unable to destroy instantiated prefab during OnMouseExit,Unable to Destroy previously instantiated object OnMouseExit() 0 Answers

Locate _MainTex from a public shader and assign to instantiated prefab for GUI 1 Answer

[Solved] Instantiated objects spawn at diferrent x 1 Answer

How do I Instantiate a prefab at the x axis position of where the UI button was clicked 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges