Search Unity

Simple question: Instantiate Prefab one second after click

Discussion in 'Scripting' started by Masterchiefs, Feb 1, 2015.

  1. Masterchiefs

    Masterchiefs

    Joined:
    Jan 10, 2015
    Posts:
    20
    Hi everybody,

    I think this is a pretty simple problem but somehow I don't see the answer.

    Let's say I have created a prefab of a simple cube with one script attached that is moving the cube upwards out of the game view when the spacebar is pressed.

    Now I would like to have another script (attached to an empty game object) that instantiates a new cube one second after the spacebar was pressed and the first cube started moving.

    What should that script look like?.
    This is what I have so far, but it does not work.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpawnScript : MonoBehaviour {
    5.    
    6.     public GameObject cube;
    7.     public Transform cubeSpawn;
    8.  
    9.     void Start ()
    10.     {
    11.         StartCoroutine (WaitForNextCube());
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         if (Input.GetKeyDown ("space")) {
    17.             WaitForNextCube();
    18.             Instantiate (cube, cubeSpawn.position, cubeSpawn.rotation);
    19.         }
    20.     }
    21.  
    22.     IEnumerator WaitForNextCube()
    23.     {
    24.         yield return new WaitForSeconds (1);
    25.     }
    26.     }
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    No, this wouldn't work. You're not calling WaitForNextCube in a coroutine, and you'd be starting the coroutine every frame, anyway. Why are you starting the Coroutine in the Start function? Do you want there to be a 1 second delay at the beginning of the game to prevent spawning?

    I don't think WaitForNextCube has much use, all it's doing is thowing a 1 second YieldInstruction.

    Here's a way:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //Instantiates a prefab after a given delay.
    5. public class SpawnScript : MonoBehaviour {
    6.  
    7.    public GameObject prefab;
    8.    public Transform spawn;
    9.  
    10.    // So that you can't press space bar multiple times
    11.    // in one second to spawn multiple prefabs.
    12.    bool isSpawning = false;
    13.  
    14.    //In case you want to change the delay to something other
    15.    //than one second.
    16.    public float delay = 1f;
    17.  
    18.    void Update() {
    19.  
    20.        if (Input.GetKeyDown("space") && !isSpawning) {
    21.            StartCoroutine(Spawn());
    22.        }
    23.  
    24.    }
    25.  
    26.     IEnumerator Spawn() {
    27.  
    28.        isSpawning = true;
    29.  
    30.        yield return new WaitForSeconds(delay);
    31.        Instantiate(prefab, spawn.position, spawn.rotiation);
    32.  
    33.        isSpawning = false;
    34.      
    35.     }
    36.  
    37.  
    38. }    
     
    Deleted User likes this.
  3. Masterchiefs

    Masterchiefs

    Joined:
    Jan 10, 2015
    Posts:
    20
    Hey thank you very very much! I guess I still have to learn a lot about coroutines:)
     
  4. Masterchiefs

    Masterchiefs

    Joined:
    Jan 10, 2015
    Posts:
    20
    Is it possible to spawn the cubes at another objects transform and make them (or one at a time) stick to the other object when it is moving? Let's say I have a sphere that is controlled by the player and when he presses space a new cube is spawned right next to it and when the sphere moves the cube sticks to it until the player presses space again. Can you do that with Hinge Joints?
     
  5. Masterchiefs

    Masterchiefs

    Joined:
    Jan 10, 2015
    Posts:
    20
    I am using this code but the cubes are simply spawned at the spheres transform and do not stick to it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveUp : MonoBehaviour {
    5.  
    6.     private HingeJoint myHingeJoint;
    7.     public Rigidbody sphere;
    8.     public float speed;
    9.  
    10.     void Start () {
    11.         myHingeJoint = gameObject.AddComponent<HingeJoint> ();
    12.         myHingeJoint.connectedBody = sphere;
    13.         myHingeJoint.breakForce = Mathf.Infinity;
    14.         myHingeJoint.breakTorque = Mathf.Infinity;
    15.  
    16.     }
    17.  
    18.     void FixedUpdate () {
    19.         if (Input.GetKeyDown("space")) {
    20.             Destroy (myHingeJoint);
    21.             rigidbody.velocity = transform.up * speed;
    22.         }
    23.     }
    24. }