Dropping Items

Hi, I currently have 2 problems with my shooter game:

  1. Items fall through the floor
  2. Items don’t work

This item is referred to as small life energy in my files and it refills your health. I know that for the first problem the reason why it’s doing that is because collision is set to trigger. I would turn it to isTrigger = false but the player needs to be able to walk through it. And yes, gravity is necessary so I added a rigidbody.

My item is a prefab, which means that I can’t attach any script to it(at least I think so). So the item won’t heal the player because it can’t call void Heal from the MMEnergy script.

Shield Attacker

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShieldAttacker : MonoBehaviour
{
    public float speed = 4f;
    public bool Turning;
    public float HP = 4f;

    public MegaBuster Charge;
    public WVS weapon;
    public MMEnergy change;

    public float dropChance;

    public GameObject smallLE;
    public GameObject bigLE;
    public GameObject smallWE;
    public GameObject bigWE;

    public Transform itemSpawn;



    // Start is called before the first frame update
    void Start()
    {

        dropChance = Random.Range(1, 128);
        Turning = false;
    }

    void Update()
    {
        if (Sub_Screen.GameIsPaused) return;

        if (Turning == true) return;

        transform.Translate(Vector3.forward * Time.deltaTime * speed);

        if (HP <= 0)
        {
            die();
        }

    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Ground")
        {
            Turning = true;

            transform.Rotate(180, 0, 0);

            StartCoroutine(Turn());



        }


        if (other.gameObject.CompareTag("Bullet"))
        {
            if (HP >= 1)
            {
                if (weapon.weaponSelected == 0)
                {
                    if (Charge.damage == 1)
                    {
                        Damage(1);
                    }
                    if (Charge.damage == 2)
                    {
                        Damage(2);
                    }
                    if (Charge.damage == 3)
                    {
                        Damage(3);
                    }
                }

                if (weapon.weaponSelected == 1)
                {
                    Damage(2);
                }


            }

            else if (HP <= 0)
            {
                die();
            }

        }
    }


    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            change.Damage(4);

        }
    }


    IEnumerator Turn()
    {

        yield return new WaitForSeconds(1);
        Turning = false;
    }



    void Damage(int attack)
    {
        dropChance = Random.Range(1, 128);
        HP -= attack;
    }

    void die()
    {
        Destroy(gameObject);
    }

    void OnDestroy()
    {
        if (dropChance >= 1 && dropChance <= 13)
        {
            Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
        }

        if (dropChance >= 14 && dropChance <= 26)
        {
            Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
        }

        if (dropChance >= 27 && dropChance <= 62)
        {
            Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
        }

        if (dropChance >= 63 && dropChance <= 72)
        {
            Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
        }
        if (dropChance >= 73 && dropChance <= 95)
        {
            Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
        }
        if (dropChance >= 100 && dropChance <= 127)
        {
            Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
        }
        if (dropChance == 128)
        {
            Instantiate(smallLE, itemSpawn.position, Quaternion.identity);
        }

    }

}

Small Life Energy

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SmallEnergy : MonoBehaviour
{
    public MMEnergy refill;
    public Rigidbody rb;

    void Awake()
    {
        // Setting up the reference.
        GameObject Health = GameObject.Find("Main Camera");
        refill = Health.GetComponent<MMEnergy>();
    }
    void start()
    {
        this.rb.useGravity = true;
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            Destroy(gameObject);
        }
        if (other.CompareTag("Ground"))
        {
            this.rb.useGravity = false;
        }
    }

    void OnDestroy()
    {
        refill.Heal(Random.Range(2, 4));

    }
}

Note: I coded a random drop chance for the item, but right now it just drops 1 thing, for debugging reasons.

Thanks in advance!

  • You can change the collision matrix so that items on specific layers can’t collide with other items of specific layers.
  • Have your script grab the player’s health script from the other variable given from OnTriggerEnter

That fixes the falling through the floor problem, but now, the player can’t pick it up anymore :(. If I set the collider to trigger, it will fall through the floor.

Welcome to your first Debugging 101 class!

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3