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 DavidDebnar · Jul 10, 2011 at 03:12 PM · instantiateraycastingcubeyield

Instantiating prefabs, when there is nothing

Hey guys. I'm at the end of my powers...I can't make one simple script :/. Simply what I need it to do, is to check, if there is no other block next it it, and if isn't instantiate. I'll post mine, it's really messy, because of trying to add dozens of vars, but it keeps instantiating even when there is one other. The algorithm should look like. If there is no block below instantiate a block below. If there is a block below that doesn't have tag "waterBlock" and if there aren't blocks on sides, instantiate blocks on sides. And repeat repeat repeat. I'm using InvokeRepeat because I wan't it to run like twice a second. And I'm using WaitForSeconds before the instantiating, so it isn't too fast. Yesterday it was working, but it was really fast, so I have put there a yield, and bam 5k cubes made in few secs :(.

CLOSED

  • David

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 Chris D · Jul 10, 2011 at 03:56 PM 0
Share

have you considered using an array to handle all the logic ins$$anonymous$$d of doing it through physics/raycasting? check out the questions on puzzle and match-3 games for examples of what I'm talking about.

avatar image DavidDebnar · Jul 10, 2011 at 04:08 PM 0
Share
  1. I suck at arrays, 2. I can't use arrays... This thing, as you may already know will be water, and I need raycasting, to get variables from other water blocks around.. $$anonymous$$aybe it would work with arrays, but I would need deeper explanation.

2 Replies

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

Answer by DavidDebnar · Jul 11, 2011 at 04:10 PM

var delay: float = 0.1; var distance: float = 1.0; // distance between centers private var pos : Vector3; var prefab : Transform; static var numBlock = 1;

var creator : Transform;

// this var is unique for all scripts //static var busy = false; // this var is unique for all scripts

InvokeRepeating("CreateBlocks", .2, .5);

function Start(){ pos = transform.position; }

//function Update(){

 //if (replicated) return;  // only replicates once
 //if (!busy){ // if nobody is replicating now
 //    CreateBlocks(); // do the replication
 //}

//}

function CreateBlocks() { if(creator == null) { Destroy(gameObject); } else { var hit:RaycastHit;

 if(Physics.Raycast(transform.position, -transform.forward, hit, distance))
 {
     if(hit.collider.tag != "waterBlock")
     {
         if(!Physics.Raycast(transform.position, -transform.right, hit, 1))
         {
             DoBlock(-transform.right);
         }
         if(!Physics.Raycast(transform.position, transform.right, hit, 1))
         {
             DoBlock(transform.right);
         }
         if(!Physics.Raycast(transform.position, transform.up, hit, 1))
         {
             DoBlock(transform.up);
         }
         if(!Physics.Raycast(transform.position, -transform.up, hit, 1))
         {
             DoBlock(-transform.up);
         }
     }
 }
 else
 {
     DoBlock(-transform.forward);
 }
 }

}

function DoBlock(offset : Vector3) { var inst = Instantiate(prefab, pos + offset, transform.rotation); inst.name = "Block"+numBlock; inst.gameObject.GetComponent(testRewrited).creator = transform; numBlock++; }

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 aldonaletto · Jul 10, 2011 at 04:42 PM

This script creates first a clone of itself in the forward direction; in the next step, it creates four clones at left, right, up and down. But each clone instantiated generates its own 5 clones (of less if blocked at any side), and these clones will generate others, and so on. If a few frames, your world will be full of replicating clones - and your frame rate will drop to zero. Another thing: you should check for neighbours and create the new blocks in the same frame, or other clones will not "see" these newcomers and instantiate others at the same positions.

EDITED: This script will replicate the original block with delay interval between each block replication. The replications will be restricted by external colliders. They replicate in all directions, but you can eliminate the unwanted ones.
This is a very tricky script: it uses the static variable busy to ensure that only one block will replicate at a time. At Update, when a block notices busy is false, it sets it to true and starts its own replication. A replication takes several frames, since a delay is started each time a side clone is created. Since busy is set, no other block will replicate until this one finishes - at the end of the replication, it clears busy . Once replicated, the block sets the replicated var, signalling to itself that it was already replicated. You can check the static var busy to know when all blocks replicated: if it's false for two consecutive Updates, there are no more blocks to replicate.

 var delay: float = 0.1;
 var distance: float = 1.1; // distance between centers
 private var pos : Vector3;
 private var replicated = false; // replicates only once
 static var numBlock = 1;  // this var is unique for all scripts
 static var busy = false;  // this var is unique for all scripts
 
 function Start(){
     pos = transform.position;
 }
 
 function Update(){
 
     if (replicated) return;  // only replicates once
     if (!busy){ // if nobody is replicating now
         CreateBlocks(); // do the replication
      }
 }
 
 function CreateBlocks(){
 
     var hit:RaycastHit;
 
     if(!Physics.Raycast(transform.position, transform.forward, hit, distance)){
         DoBlock(transform.forward);
     }
     if(!Physics.Raycast(transform.position, -transform.right, hit, 1)){
         DoBlock(-transform.right);
         yield WaitForSeconds(delay);
     }
     if(!Physics.Raycast(transform.position, transform.right, hit, 1)){
         DoBlock(transform.right);
         yield WaitForSeconds(delay);
     }
     if(!Physics.Raycast(transform.position, -transform.forward, hit, 1)){
         DoBlock(-transform.forward);
         yield WaitForSeconds(delay);
     }
     if(!Physics.Raycast(transform.position, transform.up, hit, 1)){
         DoBlock(transform.up);
         yield WaitForSeconds(delay);
     }
     if(!Physics.Raycast(transform.position, -transform.up, hit, 1)){
         DoBlock(-transform.up);
         yield WaitForSeconds(delay);
     }
     replicated = true;  // this block's replication is done
     busy = false;  // enable others to replicate
 }
 
 function DoBlock(offset : Vector3){
     var inst = Instantiate(gameObject, pos + offset, transform.rotation);
     inst.name = "Block"+numBlock;
     numBlock++;
     busy = true;
 }
Comment
Add comment · Show 9 · 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 DavidDebnar · Jul 10, 2011 at 04:51 PM 0
Share

Exactly that is my problem, that when I wanna slow down the creating or the blocks, so it not just like BA$$anonymous$$ and 125 object created, and than i run into this problem, that it sends command to instantiate, just before the another one...And nope, the framerate is pretty stable, because it creates the side blocks, just when there is a non water block below, and i have a energy system prepared, that will stops it from spreading to infinite at the sides..Any ideas, how should I make the code, so it will instantiate in a row, not instantly?

PS: sorry for grammar, but I am rly tired.

avatar image aldonaletto · Jul 10, 2011 at 05:18 PM 0
Share

Ok, let me analyze this idea - I'll be back soon.

avatar image DavidDebnar · Jul 10, 2011 at 06:56 PM 0
Share
  • bump *

avatar image aldonaletto · Jul 10, 2011 at 07:35 PM 0
Share

I edited my answer to include a replicating script. You must add this script to a single cube, and it will replicate itself one at a time while there's space available.

avatar image DavidDebnar · Jul 10, 2011 at 07:43 PM 0
Share

thanks, ill try it

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Raycasting 1 Answer

How to instantiate a custom sized object? 1 Answer

yield return new WaitForSeconds Not Working 4 Answers

Detect face of cube clicked 4 Answers

Creating a grid of cubes. 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