• 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 /
This question was closed Jul 28, 2014 at 01:11 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by TheRichardGamer · Jul 28, 2014 at 01:00 PM · instantiatetransformarrayposition

How to only instantiate objects once?

So I have a world generation script here and it's using an array to pick random blocks but I currently only have one block set up. But the problem is when it generates it creates all the blocks perfectly fine and all but the thing is that it creates like 3 times the blocks on the same position, say if I have a block [here] then there would be 2 other blocks in there occupying the space as well and that causes lag and I don't want that. How do I only create one copy on the place without creating multiple copies of already existing chunks?

Here's my script

 var width_x : int;
 var height : int;
 var width_z : int;
 var player : Transform;
 var originBlock : Transform;
 var Blocks : GameObject[];
 
 function Start () {
 
 player.transform.position.x = originBlock.transform.position.x;
 player.transform.position.y = originBlock.transform.position.y + 5;
 player.transform.position.z = originBlock.transform.position.z;
 
 }
 
 function Update () {
 
 for(var x : int = 0; x < width_x; x++){
      
 
      if(x < width_x){
      
      Instantiate(Blocks[Random.Range(0, Blocks.Length)], Vector3(x * 1, 0, 0), Quaternion.identity);
      
      }
      
           
        if(Input.GetKeyDown(KeyCode.G)){
   
   Debug.Log("Blocks: " + x);
   
   }
      
      
 
   }
   
 
 
 }
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Klarax · Jul 28, 2014 at 01:01 PM

just put a bool around it and set it to false after completion

Comment
Add comment · Show 2 · 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 TheRichardGamer · Jul 28, 2014 at 01:10 PM 0
Share

Thank you, it worked :)

avatar image Ouija · Aug 08, 2015 at 11:46 AM 0
Share

What was the working script? Im not sure what you mean by putting a bool around it. I'm trying to create an object when you press a key, but like 5 or 6 pop up at a time, i only want one. This is it so far, so how does the bool look in this script? any please would be awesome lol.

 private void Update()
                 {
                     if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Z)) {
                 Instantiate (picker);

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

How do I check the distance between multiples objects in an array from the player? 4 Answers

Make object move in a direction depending on where it spawns? (C#) 1 Answer

MissingMethodException: System.Collections.Generic.List 1 Answer

Dynamically Update Instantiated Objects Position 1 Answer

Offset Position 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