Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiate random prefab along z axis

Discussion in 'Scripting' started by jappejab, Jan 18, 2011.

  1. jappejab

    jappejab

    Joined:
    Jan 18, 2011
    Posts:
    11
    Hi! I'm trying to instantiate 5 random prefabs after eachother along the z axis.
    I have 5 pieces of a map that should randomly generate itself.
    I want to start the instantiate fiva seconds after the scene has loaded and every five seconds after that.
    Could someone please help me with this, I'm totally stuck!
     
  2. levye

    levye

    Joined:
    Dec 15, 2010
    Posts:
    19
    Do somethhing like

    if(Time.time - lastSampledTime >= 5000) { // do it }

    So that you can execute your code after passing 5 seconds.
     
  3. jappejab

    jappejab

    Joined:
    Jan 18, 2011
    Posts:
    11
    That worked great! But how do I do the instantiate part? 'Cause that's where I'm stuck
     
  4. levye

    levye

    Joined:
    Dec 15, 2010
    Posts:
    19
    Try to put your maps under Resources directory (if there is no create one) Then you can use Resources.Load to load your maps. Please check Resources.Load function for details.I hope it helps
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Welcome to the forum, Jappejab!

    If you want to make something happen at a regular time interval, you can put it in a function and use InvokeRepeating:-
    Code (csharp):
    1. function RandomPrefabFunc() {
    2.   // Instantiation code goes here...
    3. }
    4.  
    5. function Start() {
    6. // Call RandomPrefabFunc every 5 seconds.
    7.    InvokeRepeating("RandomPrefabFunc", 0, 5.0);
    8. }
    Note that time is measured in seconds rather than milliseconds in Unity, so you should not multiply the interval by 1000.
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    You would need the Instantiate part as well, I am guessing...

    Code (csharp):
    1.  
    2. Instantiate (prefab, transform.TransformPoint(Vector3(0, 0, Random.Range(-100.0, 100.0))), Quaternion.identity);
    3.  
    So this would be a random point between -100 and 100 along the Z axis of the current transform.
     
  7. jappejab

    jappejab

    Joined:
    Jan 18, 2011
    Posts:
    11
    Thanks alot! This is the script I now have but it doesn't work. What am I doing wrong?

    Code (csharp):
    1.  
    2. var mapPiece : Rigidbody[];
    3.  
    4.  
    5. function RandomPrefabFunc() {
    6. Instantiate (Rigidbody, transform.TransformPoint(Vector3(0, 0, Random.Range(-100.0, 100.0))), Quaternion.identity);
    7. }
    8.  
    9. function Start() {
    10. // Call RandomPrefabFunc every 5 seconds.
    11.    InvokeRepeating("RandomPrefabFunc", 0, 5.0);
    12. }
     
  8. enragedmrt

    enragedmrt

    Joined:
    Oct 5, 2009
    Posts:
    95
    mapPiece is declared as a Rigidbody array, so you must access the array locations.
    Try:

    Instantiate (mapPiece[Random.Range(0, mapPiece.length)], transform.TransformPoint(Vector3(0, 0, Random.Range(-100.0, 100.0))), Quaternion.identity);

    I could be wrong on syntax, but that ought to do it.
     
  9. jappejab

    jappejab

    Joined:
    Jan 18, 2011
    Posts:
    11
    Okey, that helped a bit. But the prefab still won't instantiate, my current code is:

    Code (csharp):
    1. var mapPiece : Rigidbody[];
    2.  
    3.  
    4. function RandomPrefabFunc() {
    5. Instantiate (mapPiece[Random.Range(0, mapPiece.length)], transform.TransformPoint(Vector3(0, 0, Random.Range(-100.0, 100.0))), Quaternion.identity);
    6. }
    7.  
    8. function Start() {
    9. // Call RandomPrefabFunc every 5 seconds.
    10.    InvokeRepeating("RandomPrefabFunc", 0, 5.0);
    11. }
     
    Last edited: Jan 18, 2011
  10. enragedmrt

    enragedmrt

    Joined:
    Oct 5, 2009
    Posts:
    95
    Need a bit more information bud. Lol. Is there an error? Are you sure it isn't instantiating and you just can't see it?

    Note, I just copy/paste your code into my own unity file and it worked as expected. Make sure in the inspector you are filling out your mapPiece array with rigidbodies. Also, make sure you can see them! Lol.
     
  11. jappejab

    jappejab

    Joined:
    Jan 18, 2011
    Posts:
    11
    i restarted Unity and now it works xD
     
  12. jappejab

    jappejab

    Joined:
    Jan 18, 2011
    Posts:
    11
    Okey, so the instantiate function works perfect, now I want it to create the prefabs at a set position. That position is the same as the objekt the script I put on, plus 10 in the z axis. How do I do this?
     
  13. enragedmrt

    enragedmrt

    Joined:
    Oct 5, 2009
    Posts:
    95
    You do realize that forum users aren't going to just write the script for you right? You have to learn and figure some things out on your own. When there's a hang up it's great to ask a question, but it isn't realistic to keep asking until the project is done. Look at the docs, it's how I learned. Read some tutorials, unity is wide-spread by now and they're everywhere. Then get back to your project.

    But to answer your question, currently the script instantiates them in a range along the z that is +/- 100 from the center of the GameObject the script is attached to. If you want it to spawn always at position+10,
    Code (csharp):
    1. Instantiate (mapPiece[Random.Range(0, mapPiece.length)], transform.TransformPoint(Vector3(0, 0, 10)), Quaternion.identity);
    Or if you mean the +/- range occurs with it's center at position+10, then use
    Code (csharp):
    1. Instantiate (mapPiece[Random.Range(0, mapPiece.length)], transform.TransformPoint(Vector3(0, 0, 10+Random.Range(-100.0, 100.0))), Quaternion.identity);