Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Random instantiate with while loop

Discussion in 'Scripting' started by rame16, Nov 27, 2012.

  1. rame16

    rame16

    Joined:
    Nov 21, 2012
    Posts:
    29
    Hello , i'm using a script to instantiate some objects over random positions:

    Code (csharp):
    1. // Instantiates prefab somewhere between -10.0 and 10.0 on the x-z plane
    2. var prefab : GameObject;
    3. var x : int;
    4. var i = 0;
    5.  
    6.  
    7. function Start () {
    8.  
    9. var position: Vector3 = Vector3(Random.Range(-10.0, 10.0), 0, Random.Range(-10.0, 10.0));
    10.  
    11.  
    12.  
    13.      if(x > 0) // condition de base: que le nombre ne soit pas encore épuisé
    14.      while ( i < x ) //tant que x est toujours + grand que zero
    15.         {
    16.         Instantiate(prefab, Vector3(Random.Range(-10.0, 10.0), 0, Random.Range(-10.0, 10.0)), Quaternion.identity);
    17.         i ++;
    18.         }
    19.        
    20. }
    it works really fine, but now i'm trying to raycast each one of them clones to stick it to the floor; i'm just learning so i found a code which works fine alone:

    Code (csharp):
    1.     var hit : RaycastHit;
    2.  
    3.     if (Physics.Raycast (this.transform.position, -Vector3.up, hit)) {
    4.        distanceToGround = hit.transform.position;
    5.        this.transform.position = hit.point;
    6.     }
    7.  
    but, i have to assign this to every clone i generate, so i tried to insert a 'if' in it, like following:

    Code (csharp):
    1. while ( i < x ) //tant que x est toujours + grand que zero
    2.         {
    3.        
    4.             Instantiate(prefabinst, Vector3(Random.Range(-10.0, 10.0),0, Random.Range(-10.0, 10.0)), Quaternion.identity);
    5.             if (Physics.Raycast (prefabinst.transform.position, -Vector3.up, hit)) {
    6.             distanceToGround = hit.transform.position;
    7.              print("Distance: "+distanceToGround);
    8.             }
    9.             i ++;
    10.         }
    but looks like it raycasts for the first of them only, whereas i'd like each clone to detect the floor, and then stick on it; apparently i can't make this operation for each instance in the 'while' loop, i'm not sure if i should use 'foreach', ..
    For now i've put the 'stick' script on the base prefab, and it works, but i was thinking maybe of a better, less demanding way?
     
  2. pat_sommer

    pat_sommer

    Joined:
    Jun 28, 2010
    Posts:
    586
    i "think" i know what your trying to do, but I'm not sure so forgive me if its not lol also this code isn't tested, so just use the idea of it and try to put it in your code,

    you want each instance you make to then cast a ray down to the ground right? if so you can store each instance as a variable like this

    Code (csharp):
    1.  
    2.  
    3.     var thisPrefab = Instantiate(prefab, transform.position, transform.rotation)
    4.  
    5.     if (Physics.Raycast (thisPrefab.transform.position, -Vector3.up, hit))
    6.    {
    7.     distanceToGround = hit.transform.position;
    8.    }
    9.  
    10.  
     
  3. rame16

    rame16

    Joined:
    Nov 21, 2012
    Posts:
    29
    It looks really promising, thanks, i will try this.
     
  4. rame16

    rame16

    Joined:
    Nov 21, 2012
    Posts:
    29
    It works well!

    what i did now, thanks to some tests and researchs is:


    Code (csharp):
    1.  
    2.  
    3. if (Physics.Raycast (thisPrefab.transform.position, -Vector3.up, hit)) {
    4.            
    5.             distanceToGround = hit.transform.position;
    6.             thisPrefab.transform.position = hit.point;
    7.            
    8.            
    9.             }
    10.  

    which gives me a precise distance from the closest floor and stick each instance on it! this is absolutely perfect. I now have to take care of the rotation of each instance, THanks for your Help, sir!