2D Catcher

So I’ve got a lot of the mechanics already done, however some weird things are at hand currently in my progression… Also, there’s some things I’d like explained as well.

Much of this will be described in the attachments below so check them out. (I confuse myself sometimes when explaining things like this)

Main elements of this game include:

  • Items that fall on a randomized horizontal range
  • Score
  • Life
  • Layered sprites
  • Horizontal movement
  • 10+ different Items

Basically, you’re just trying to catch Items as they fall.
There is a 2D collider on every Item, as well as the player for physics.
There is a copy of the collider on a gameobject parented to the item. This is a trigger so if they pass the red line or a player catches them, they will be destroyed. Yes They were both set to origin first.
The players box collider inside is for the items when they are caught. This is also a trigger.

Here’s my scripts:

Movement

using UnityEngine;
using System.Collections;

public class BucketMovement : MonoBehaviour
{
    public float minX = -4.5f;
    public float maxX = 4.5f;
    public float speed = 5.0f;

    private Rigidbody2D playerRigid;

    void Start ()
    {
        playerRigid = gameObject.GetComponent<Rigidbody2D> ();
   
   
    }
   

    void FixedUpdate ()
    {

        if (Input.GetKey (KeyCode.LeftArrow)) {
            //transform.position -= transform.right * speed * Time.deltaTime;
            playerRigid.velocity = new Vector2 (-speed, 0) * speed * Time.deltaTime;
        }

        else
        {
            playerRigid.velocity = new Vector2 (0, 0) * speed * Time.deltaTime;
        }

        if (Input.GetKey (KeyCode.RightArrow))
        {
            //transform.position += transform.right * speed * Time.deltaTime;
            playerRigid.velocity = new Vector2 (speed, 0) * speed * Time.deltaTime;
        }


        // Constraints player movement to 'minX' and 'maxX'
        if (playerRigid.position.x <= minX)
        {
            playerRigid.position = new Vector2 (minX, playerRigid.position.y);
        }
        if (playerRigid.position.x >= maxX)
        {
            playerRigid.position = new Vector2 (maxX, playerRigid.position.y);
        }
    }
}

Item Spawner

using UnityEngine;
using System.Collections;

public class ItemSpawner : MonoBehaviour
{
    public float rateOfSpawn = 2.0f;
    public float minSpawnX = -5.11f;
    public float maxSpawnX = 5.11f;
    public float spawnHeight = 4.5f;

    public GameObject[] Item_List;



    void Start ()
    {
    
    }

    void Update ()
    {
        Invoke ("SpawnItemsIn", rateOfSpawn);
    }

    void SpawnItemsIn ()
    {
        int Items_In_List = Random.Range (0, 9);

        Vector2 spawn_Pos = new Vector2 (Random.Range (minSpawnX, maxSpawnX), spawnHeight);

        Instantiate (Item_List [Items_In_List], spawn_Pos, Quaternion.identity);
    }
}

I’m getting this error, I have no Idea how to fix this? This is the first time I’ve used arrays in unity/C# so that may be a contributor…

And last but not least, it spawns like crazy! It’s actually pretty hilarious to see every prefab item just fall from the sky an extreme number of times. It lags after like 30 seconds because they arent destroying below the player (Haven’t gotten to that yet.)

Any ideas? How would I make a spawn rate time for the items to fall?
Any improvement to my code that can be explained? Thanks a lot guys.
Here’s some poorly drawn examples.

2658201--187430--Example.jpg
2658201--187431--Exp2.jpg

How often are you seeing that error you posted? I’m guessing one or more items in “Item_List” is null. I recommend adding like a

void Start ()
{
  Debug.Log(Item_List);
}

to your start method so you can inspect what’s in your array. It’s hard to know what the problem is without seeing the contents of the array.

And to answer your second question.

  • What you are currently doing is spawning an item every time “Update” is called (which is a lot) but just delaying each spawn by 2.0 seconds.
  • What you want to do is add a cool down so that “SpawnItemsIn” only gets called from Update every 2.0 seconds.
float countdown = 0.0f;
float cooldown = 2.0f;

void Update ()
{
  if (countdown > 0.0f) {
    countdown -= Time.deltaTime;
  } else {
     countdown = cooldown;
     SpawnItemsIn();
  }
}

Once update is called for the first time we are going to call SpawnItemsIn and set the cooldown to 2.0 seconds. After that every time we call update we are going to subtract a little more time until we hit 0 seconds and spawn and reset the timer.