- Home /
How to make an ammo box script give ammo(change integers) in another script attached to a child of child of a collider.
I have an Ammo Box script attached to an ammo box model. It is set up to where if the player collides with the box, it will give the player ammo and destroy itself. The ammo box script needs a reference to a gun script that is on a gun model. The gun model is a child of a camera, which is a child of the player's collider. The problem is that the ammo box doesn't give the player ammo nor destroys itself, and it gives me an error saying, "NullReferenceException: Object reference not set to an instance of an object AmmoBox.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/AmmoBox.cs:14)"
Sorry about the huge scripts.
Here's the Ammo Box script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AmmoBox : MonoBehaviour {
public int LittleBullets;
public int BigBullets;
public int MacroBullets;
public int rockets;
public int grenades;
void OnTriggerEnter (Collider other) {
Gun gun = other.gameObject.GetComponentInChildren<Camera>().GetComponentInChildren<Gun>();
if(gun == null)
{
Debug.LogError("No Gun component found!");
return;
}
if (gun.takesLittleBullets)
{
gun.ammo += LittleBullets;
}
if (gun.takesBigBullets)
{
gun.ammo += BigBullets;
}
if (gun.takesMacroBullets)
{
gun.ammo += MacroBullets;
}
if (gun.takesRockets)
{
gun.ammo += rockets;
}
gun.grenadeCount += grenades;
gun.currentAmmo = gun.magSize;
gun.currentAmmoText.text = gun.currentAmmo + "/" + gun.magSize;
gun.ammoText.text = gun.ammo.ToString();
Debug.Log("Refilled Ammo!");
Destroy(gameObject);
}
}
And here's the Gun script that is attached to a child of a child of the collider gameobject:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Gun : MonoBehaviour {
public float damage = 10f;
public float range = 100f;
public float impactForce;
public float fireRate = 10f;
public Camera mainCam;
public ParticleSystem muzzleFlash;
public List<GameObject> impactEffects = new List<GameObject>();
public Animation animations;
public AudioSource audioSource;
public AudioClip fireSoundEffect;
public AudioClip reloadSoundEffect;
public Text currentAmmoText;
public Text ammoText;
public bool loadedOnStart;
public bool takesLittleBullets;
public bool takesBigBullets;
public bool takesMacroBullets;
public bool takesRockets;
public int magSize;
public int ammo;
public int grenadeCount;
public int currentAmmo;
private float nextTimeToFire = 0f;
void Start()
{
if (loadedOnStart)
{
currentAmmo = magSize;
}
else
{
currentAmmo = 0;
}
currentAmmoText.text = currentAmmo + "/" + magSize;
ammoText.text = ammo.ToString();
}
void Update () {
if (Input.GetMouseButton(0) && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
if (Input.GetKeyDown(KeyCode.R))
{
Reload();
}
}
void Shoot()
{
if (animations.IsPlaying("reload"))
{
return;
}
if(currentAmmo == 0)
{
return;
}
animations.Stop();
audioSource.Stop();
muzzleFlash.Play();
animations.Play("fire");
audioSource.clip = fireSoundEffect;
audioSource.Play();
currentAmmo--;
currentAmmoText.text = currentAmmo + "/" + magSize;
RaycastHit hit;
if(Physics.Raycast(mainCam.transform.position, mainCam.transform.forward, out hit, range))
{
int impactIndex = 0;
foreach(GameObject impactEffect in impactEffects)
{
if(hit.transform.tag == impactEffect.transform.tag)
{
impactIndex = impactEffects.IndexOf(impactEffect);
}
}
Health health = hit.transform.GetComponent<Health>();
if(health != null)
{
health.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffects[impactIndex], hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 1f);
}
}
void Reload()
{
if (animations.IsPlaying("reload"))
{
return;
}
if(currentAmmo == magSize)
{
return;
}
if(ammo == 0)
{
return;
}
audioSource.Stop();
animations.Play("reload");
audioSource.clip = reloadSoundEffect;
audioSource.Play();
int ammoToTake = magSize - currentAmmo;
if (ammo < magSize && ammo > 0 && ammo <= ammoToTake)
{
currentAmmo += ammo;
ammo = 0;
}
if(ammo >= magSize || ammo > ammoToTake)
{
currentAmmo = magSize;
ammo -= ammoToTake;
}
ammoText.text = ammo.ToString();
currentAmmoText.text = currentAmmo + "/" + magSize;
}
}
Your answer
Follow this Question
Related Questions
OnMouseDown with colliders behind the object being clicked 3 Answers
Multiple Cars not working 1 Answer
Problem with acing problems with : OnTriggerEnter 0 Answers
On collision nearby objects ui view (scroll view) 0 Answers
Detecting mouse over PNG 0 Answers