Ammo pick up

Hello there everyone. Today I am working on my ammo pick ups for my game! I have the object being picked up and everything, now I just need it to modigy/add ammo to my guns. They all use the same ammo etc right now so don’t need that. Just need a way when I enter the collider, it not only destorys the object but adds the Ammo. Going to put my Gun script which holds my ammo and then my poweruptrigger script.

GUN SCRIPT:

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (AudioSource))]
public class Gun : MonoBehaviour {
   
    public enum GunType {Semi,Burst,Auto};
   
    public LayerMask collisionMask;
    public float gunID;
    public GunType gunType;
    public float rpm;
    public float damage = 1;
   
    public int totalAmmo = 40;
    public int ammoPerMag = 10;
   
    // Components
    public Transform spawn;
    public Transform shellEjectionPoint;
    public Rigidbody shell;
    private LineRenderer tracer;
   
    [HideInInspector]
    public GameGUI gui;
   
    // System:
    private float secondsBetweenShots;
    private float nextPossibleShootTime;
    public int currentAmmoInMag;
    private bool reloading;
   
    void Start() {
        secondsBetweenShots = 60/rpm;
        if (GetComponent<LineRenderer>()) {
            tracer = GetComponent<LineRenderer>();
        }
       
        currentAmmoInMag = ammoPerMag;
       
        if (gui) {
            gui.SetAmmoInfo(totalAmmo,currentAmmoInMag);
        }
    }
   
    public void Shoot() {
       
        if (CanShoot()) {
            Ray ray = new Ray(spawn.position,spawn.forward);
            RaycastHit hit;
           
            float shotDistance = 20;
           
            if (Physics.Raycast(ray,out hit, shotDistance, collisionMask)) {
                shotDistance = hit.distance;
               
                if (hit.collider.GetComponent<Entity>()) {
                    hit.collider.GetComponent<Entity>().TakeDamage(damage);
                }
            }
           
            nextPossibleShootTime = Time.time + secondsBetweenShots;
            currentAmmoInMag --;
           
            if (gui) {
                gui.SetAmmoInfo(totalAmmo,currentAmmoInMag);
            }
           
            audio.Play();
           
            if (tracer) {
                StartCoroutine("RenderTracer", ray.direction * shotDistance);
            }
           
            Rigidbody newShell = Instantiate(shell,shellEjectionPoint.position,Quaternion.identity) as Rigidbody;
            newShell.AddForce(shellEjectionPoint.forward * Random.Range(150f,200f) + spawn.forward * Random.Range(-10f,10f));
        }
       
    }
   
    public void ShootContinuous() {
        if (gunType == GunType.Auto) {
            Shoot ();
        }
    }
   
    private bool CanShoot() {
        bool canShoot = true;
       
        if (Time.time < nextPossibleShootTime) {
            canShoot = false;
        }
       
        if (currentAmmoInMag == 0) {
            canShoot = false;
        }
       
        if (reloading) {
            canShoot = false;
        }
       
       
        return canShoot;
    }
   
    public bool Reload() {
        if (totalAmmo != 0 && currentAmmoInMag != ammoPerMag) {
            reloading = true;
            return true;
        }
       
        return false;
    }
   
    public void FinishReload() {
        reloading = false;
        currentAmmoInMag = ammoPerMag;
        totalAmmo -= ammoPerMag;
       
        if (totalAmmo < 0) {
            currentAmmoInMag += totalAmmo;
            totalAmmo = 0;
        }
       
        if (gui) {
            gui.SetAmmoInfo(totalAmmo,currentAmmoInMag);
        }
    }
   
    IEnumerator RenderTracer(Vector3 hitPoint) {
        tracer.enabled = true;
        tracer.SetPosition(0,spawn.position);
        tracer.SetPosition(1,spawn.position + hitPoint);
       
        yield return null;
        tracer.enabled = false;
    }
}

PowerUpTrigger Script:

using UnityEngine;
using System.Collections;

public class PowerUpTrigger : MonoBehaviour {


    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    }

    public void OnTriggerEnter(Collider other)
    {
        Debug.Log ("Entered Collider!~" + gameObject.name);
        if(gameObject.name.StartsWith("AmmoPowerUp"))
        {
            Debug.Log ("Entered");
            GameObject.Destroy(gameObject);

            //add a bonus here
        }
        //Do for each power up

    }
}

I can’t figure out how to do this :frowning:

This is part of game development. You need to figure this out otherwise it won’t be your game!

You’ve already broken down the problem well enough to be able to figure it out.

  1. When the player enters the ammo collider
  2. Update the player’s ammo
  3. Destroy the ammo object

Walk through each step of the problem and solve them one at a time. If you can’t figure one out, break it down further. For example, it seems like you are stuck on #2. So break that step down into its simplest parts and begin to solve those.

This isn’t a difficult problem, but people aren’t going to answer requests to write your code for you.

That being said if you want help after trying again, message me and we can walk through the problem.

You need to reference the player somehow. There are a lot of ways of doing that.

you can use the collider “other” to get a reference to the gameobject that collided with the powerup. You can check tags to see if it’s the player. Then there are functions to find children of that game object (I’m assuming the weapon is a child of the player) and then you have your reference. Then you’ll just need a function in the gunscript to add ammo which can be called.