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 /
avatar image
0
Question by LeePorter · Jul 10, 2019 at 10:44 AM · instantiatebeginner

how do I make it so my building prefab instantiates only once

I'm trying to make something where when a button is pressed a building prefab is instantiated and appears in a position on a globe. I only want 1 building to be spawned and no more. i kind of know what i need to do but I'm not sure how to put it into code as i am a beginner and just learning as i go.

here is the script i am using to instantiate the buildings

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Spawner : MonoBehaviour
 {
     public Transform[] spawnLocations;
     public GameObject[] WhatToSpawnPrefab;
     public GameObject[] WhatToSpawnClone;
     
 
 
     public void Update()
 
     {
      
 
 
      SpawnBuilding();
 
    
 
         void SpawnBuilding()
 
             
         {
             if (Input.GetKeyDown("1"))
 
             {
                 GameObject go = Instantiate(WhatToSpawnPrefab[0], spawnLocations[0].transform.position, Quaternion.Euler(85, 90, 360)) as GameObject;
                 go.transform.parent = GameObject.Find("Globe").transform;
             }
 
           
 
         }
     }
 
 }

any help would be greatly appreciated!

Comment
Add comment
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

2 Replies

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

Answer by Ak0rn · Jul 10, 2019 at 10:51 AM

could have a bool called alreadyInstantiated or something and then change your code to this: if (Input.GetKeyDown("1") && !alreadyInstantiated) { alreadyInstantiated = true; GameObject go = Instantiate(WhatToSpawnPrefab[0], spawnLocations[0].transform.position, Quaternion.Euler(85, 90, 360)) as GameObject; go.transform.parent = GameObject.Find("Globe").transform; } else {alreadyInstantiated = false;}

Comment
Add comment · Show 4 · 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 LeePorter · Jul 10, 2019 at 12:40 PM 0
Share

its spawning each time i press the button to spawn the building, i only want there to be one on the globe

avatar image Ak0rn LeePorter · Jul 10, 2019 at 01:43 PM 0
Share

sorry that's me being stupid, get rid of the "else" part. That should fix it. If not I'll take a closer look in a $$anonymous$$ute

avatar image LeePorter Ak0rn · Jul 10, 2019 at 01:45 PM 0
Share

brilliant! that worked thanks! :)

Show more comments
avatar image
1

Answer by ertunaozderya · Jul 10, 2019 at 10:54 AM

i am beginner too but you can try to write your codes into start method.

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 Ak0rn · Jul 10, 2019 at 11:19 AM 0
Share

I don't think he wants it to spawn automatically, I think he wants it to only spawn when the player presses a button

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

144 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[SOLVED]: Bullets passing through platforms - Brackeys Tutorial 1 Answer

How to instantiate a sprite after it has been destroyed? 2 Answers

Instantiate not working? 0 Answers

why do my instantiated prefabs spawn depending on the position / rotation of the globe? 1 Answer

How to make an Array equal the Components of an Instantiated GameObject 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