Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
-1
Question by nicktheuma · Sep 18, 2012 at 04:48 PM · randomblock

I need help with a Block Spawner

I am trying to make a block which can spawn different blocks..I am not very good at coding unfortunately..can anyone help? this is what i managed till now (its in java script):

 #pragma strict
 
 function Start () {
 
 }
 
 static function Instantiate( original : Object, position : Vector3, rotation : Quaternion ) : Object {
     transform.position = Random.insideUnitSphere * 10;
 }
 
 function Update () {
 
 }

Comment
Add comment · Show 2
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 dorpeleg · Sep 18, 2012 at 05:10 PM 0
Share

Hi I see you'r still struggling... You should tell us what you are missing so we can help :)

avatar image AlucardJay · Sep 18, 2012 at 06:35 PM 0
Share

I just formatted your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window =]

http://answers.unity3d.com/questions/317948/how-do-you-make-a-spawner-block.html

3 Replies

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

Answer by AlucardJay · Sep 18, 2012 at 07:16 PM

You need to break it down ....

  • 1

Let's first look at Instantiate : http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

From the Unity Script Reference above we get an example script, and can make a simple script :

 var prefabBlock : Transform;
 
 function Start()
 {
     // Instantiates 10 copies of prefabBlock each 2 units apart from each other
     for ( var i : int = 0; i < 10; i ++ ) 
     {
         Instantiate( prefabBlock, Vector3(i * 2.0, 0, 0), Quaternion.identity );
     }
 }

  • 2

Great, so at start 10 blocks are spawned, 2 units apart from each other though! Let's make them spawn at a specific place. You want to use a 'Spawn Block', so add that to the script, then use its position in the Instantiate. Make sure you have a 'SpawnBlock' on the stage, then drag it into the script's inspector :

 var spawnBlock : Transform;
 
 var prefabBlock : Transform;
 
 function Start()
 {
     // Instantiates 10 copies of prefabBlock each 2 units apart from each other
     for ( var i : int = 0; i < 10; i ++ ) 
     {
         Instantiate( prefabBlock, spawnBlock.position, Quaternion.identity );
     }
 }

Now all 10 cubes should spawn at the same position as the spawnBlock object.

  • 3

Next to look at making it more complex, let's change it so that it spawns a block every time the SpaceBar is pressed. We can put the instantiate in a function, and then just call that function when we want a block. Then we need to check every update for if the SpaceBar has been pressed :

 var spawnBlock : Transform;
 var prefabBlock : Transform;
 
 var spawnTime : float = 3.0;
 
 function Update()
 {
     if ( Input.GetKeyDown(Keycode.Space) )
     {
         SpawnTheBlock();
     }
 }
 
 function SpawnTheBlock()
 {
     Instantiate( prefabBlock, spawnBlock.position, Quaternion.identity );
 }

  • 4

Now we can spawn enemies, but how to make it so the enemys are spawned by themselves, and at random intervals? For this, there is a handy command called InvokeRepeating : http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.InvokeRepeating.html

 var spawnBlock : Transform;
 var prefabBlock : Transform;
 
 var spawnTime : float = 3.0;
 
 function Start()
 {
     InvokeRepeating( "SpawnTheBlock", 0.1, spawnTime );
 }
 
 function SpawnTheBlock()
 {
     Instantiate( prefabBlock, spawnBlock.position, Quaternion.identity );
 }

  • 5

Hey, it's nearly working, but how many Blocks have been spawned? Let's add a counter :

 var spawnBlock : Transform;
 var prefabBlock : Transform;
 var spawnTime : float = 3.0;
 
 var spawnCount : int = 3.0;
 
 function Start()
 {
     InvokeRepeating( "SpawnTheBlock", 0.1, spawnTime );
 }
 
 function SpawnTheBlock()
 {
     Instantiate( prefabBlock, spawnBlock.position, Quaternion.identity );
     spawnCount += 1; // Note : this can be written as spawnCount ++;
 }
 
 function OnGUI()
 {
     GUI.Box( Rect( (Screen.width * 0.5) - 50, 10, 100, 25 ), "Blocks : " + spawnCount.ToString() );
 }

  • 6

Nice, ..... hey wait! there are too many Blocks! Lets cap the amount of blocks that can be spawned. We need to stop the InvokeRepeating : http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.CancelInvoke.html

 var spawnBlock : Transform;
 var prefabBlock : Transform;
 var spawnTime : float = 3.0;
 var spawnCount : int = 3.0;
 
 var maximumSpawnCount : int = 10;
 
 function Start()
 {
     InvokeRepeating( "SpawnTheBlock", 0.1, spawnTime );
 }
 
 function SpawnTheBlock()
 {
     Instantiate( prefabBlock, spawnBlock.position, Quaternion.identity );
     spawnCount += 1; // Note : this can be written as spawnCount ++;
     
     if ( spawnCount >= maximumSpawnCount )
     {
         CancelInvoke( "SpawnTheBlock" );
     }
 }
 
 function OnGUI()
 {
     GUI.Box( Rect( (Screen.width * 0.5) - 50, 10, 100, 25 ), "Blocks : " + spawnCount.ToString() );
 }

  • 7

Phew, now only 10 blocks spawn. Wow, this is all pretty good. But I forgot something, you want to spawn a different random block each time. For this we need places to put the blocks, and a Random number to choose a block, then if that : http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html

 var spawnBlock : Transform;
 
 var prefabBlock1 : Transform;
 var prefabBlock2 : Transform;
 var prefabBlock3 : Transform;
 
 var spawnTime : float = 3.0;
 var spawnCount : int = 3.0;
 var maximumSpawnCount : int = 10;
 
 function Start()
 {
     InvokeRepeating( "SpawnTheBlock", 0.1, spawnTime );
 }
 
 function SpawnTheBlock()
 {
     var rnd : int = Random.Range( 1, 4 ); // 1 to 3 is written 1,4 as the 4 is never returned, see the docs
     
     if ( rnd == 1 )
     {
         Instantiate( prefabBlock1, spawnBlock.position, Quaternion.identity );
     }
     else if ( rnd == 2 )
     {
         Instantiate( prefabBlock2, spawnBlock.position, Quaternion.identity );
     }
     else if ( rnd == 3 )
     {
         Instantiate( prefabBlock3, spawnBlock.position, Quaternion.identity );
     }
     
     spawnCount ++;
     
     if ( spawnCount >= maximumSpawnCount )
     {
         CancelInvoke( "SpawnTheBlock" );
     }
 }
 
 function OnGUI()
 {
     GUI.Box( Rect( (Screen.width * 0.5) - 50, 10, 100, 25 ), "Blocks : " + spawnCount.ToString() );
 }

And there we have it, one multi-random-block spawning script, to spawn at BigBlocks position =]

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

Answer by ThreeDeeZ · Sep 18, 2012 at 06:53 PM

To spawn different blocks (prefabs) you are going to need a way to provide your spawn method a list or some form of set to choose from.

Example in C#:

 //Somewhere in your setup you would need to define the prefabs for each block type...in this case all would be subclassed from CustomBlockThing.
 Public List<CustomBlockThing> BlockTypesList = new List<CustomBlockThing>();
 
 //...assume we have populated the list. Since the list is set to Public, you could do so by dragging objects into it in the editor.
 
 //Here is our method to spawn new blocks. It selects 1 randomly from the types of blocks available in the previously declared BlocksList
 GameObject SpawnBlocks(int blockType = -1){
 
     //This allows us to specify the type if we do not want random...
     if (blockType == -1){
 
         CustomBlockThing randomBlock;
 
         //Generate a random number in the range of 1 to the number of block types we have defined.
         int randomBlockType = Random.Range(1, BlockTypes.length);
         
         
         //Here we use that random number to select a block type to spawn
         randomBlock = Instantiate(BlockTypes[randomBlockType], transform.position, transform.rotation) as CustomBlockThing;
 
         //Pass a reference to the newly created random typed CustomBlockThing to the caller
         return randomBlock;
     }
     else{
         
         //should probably generate an error of some type if the blockType is out of range, but I will instead assume we ALWAYS return a block of some type...
         if (blockType > BlockTypes.length || blockType < 1){
             CustomBlockThing randomBlock;
 
             //Generate a random number in the range of 1 to the number of block types we have defined.
             int randomBlockType = Random.Range(1, BlockTypes.length);
 
             //Here we use that random number to select a block type to spawn
             randomBlock = Instantiate(BlockTypes[randomBlockType], transform.position, transform.rotation) as CustomBlockThing;
             
             //Pass a reference to the newly created random typed CustomBlockThing to the caller
             return randomBlock;
         }
         
         CustomBlockThing specificBlock;
         
         //note that we still use the list, but ask for a specific item/index based on the blockType variable.
         randomBlock = Instantiate(BlockTypes[randomBlockType] transform.position, transform.rotation) as CustomBlockThing;
 
         //Pass a reference to the newly created, specific type of, CustomBlockThing to the caller
         return specificBlock;
     }
 }


If you are simply randomizing the values for each block as it is spawned, you may not need to do the above, but instead something like (psueudo-code)

 newBlock = instantiate a new block object at position and heading
 
 newBlock.height = some random value in a limited range
 newBlock.position = some random offset
 newBlock.velocity = ...
 newBlock.otherProperty = some other random value
 
 done.
 
Comment
Add comment · 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
0

Answer by nicktheuma · Sep 18, 2012 at 06:53 PM

thanks!, actually i managed abit further..the biggest problem I have now is rendering all the objects I have..it takes forever..and im planning to add many more..

 #pragma strict
 
 function Start () {
 
 }
 
 
 // Instantiates 100 copies of prefab each 2 units apart from each other
 
 var prefab : Transform;
 var prefab1 : Transform;
 var prefab2 : Transform;
 
 for (var i : int = 0;i < 100000000; i++) {
     Instantiate (prefab, (Random.insideUnitSphere * 5), Quaternion.identity);
 }
 
 for (var j : int = 0;j < 100000000; j++) {
     Instantiate (prefab1, (Random.insideUnitSphere * 5), Quaternion.identity);
 }
 
 for (var k : int = 0;k < 100000000; k++) {
     Instantiate (prefab2, (Random.insideUnitSphere * 5), Quaternion.identity);
 }
 
 
 
 
 function Update () {
 
 }
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 Screenhog · Sep 18, 2012 at 06:59 PM 0
Share

Well, yeah, that takes forever. You're instantiating each prefab 100 million times. Unity should be crashing at that kind of code.

avatar image AlucardJay · Sep 18, 2012 at 07:19 PM 0
Share

yep, 100000000 is a little more than 100 ! Check my answer, it may help with program$$anonymous$$g in general ...

here's some links for the beginner :

Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/

Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/

the Unity Wiki : http://wiki.unity3d.com/index.php/Tutorials

A list of resources : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html

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

12 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

Related Questions

instantiating at a random time 1 Answer

Randomize Audio Clip 1 Answer

Weapon random movement 0 Answers

Perlin Noise Questions 1 Answer

Cant Destroy specific object 2 Answers


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