Search Unity

Spawning Objects after the Character has moved in the x direction

Discussion in 'Scripting' started by DarkNeo, Jul 15, 2014.

  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Hi there,

    I have the following script which is spawning my objects perfectly fine, however to make sure the spawned objects do not stack up if the player is idle, I was thinking about making it so after the player has moved in the x direction Then the objects will start to spawn.

    I have tried altering the code in the Start() and that is all I can come up with.. it gives me no errors, but my objects still continue to spawn on start, Do I need to use another Function?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SpawnScript : MonoBehaviour {
    6.  
    7.     public GameObject[] obj;
    8.     public float spawnMin = 1f;
    9.     public float spawnMax = 3f;
    10.  
    11.     // Use this for initialization
    12.     void Start ()
    13.     {
    14.         if (gameObject.tag == "birdSpawn" == (transform.position.x < 50.0));
    15.         {
    16.             Spawn();
    17.         }
    18.  
    19.     }
    20.  
    21.     void Spawn()
    22.     {
    23.         Instantiate(obj[Random.Range (0, obj.GetLength(0))], transform.position, Quaternion.identity);
    24.         Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
    25.     }
    26.  
    27. }
    28.  
    If I cannot get my current script above to work, I am thinking of making an invisible hitbox and when the character enters then my objects will start to spawn.

    If anyone could help/steer me in correction direction that would be super! Thanks folks :)
     
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Typo there, you'll need a && operator instead of ==.
    Also you might possibly have a startup position which matches the condition so that spawning starts right away.
    I guess it's better to store the startup condition somewhere and execute your spawning code in a Coroutine which checks if the position has changed - Start() is only called once, meaning that if you fix the typo you might possibly not see any spawning at all.
     
    DarkNeo likes this.
  3. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Adjusted the operator, and it still manages to spawn, no errors from my script.

    Will have a look at that. Cheers mate