- Home /
problem with ammo count
i need to make an ammo counter for a little game i'm making.. basically the ammo counter works, it's linked to the bullet spawn:
using UnityEngine;
using System.Collections;
public class Freccia : MonoBehaviour {
public AudioClip suonoScocca;
public AudioClip scarico;
public Rigidbody Frecciadalanciare;
public Transform Spawn;
public float velocitafreccia = 50;
public int ammo=100;
void Update ()
{
if (Input.GetMouseButtonDown (0)){
ammo-=1;
Scocca ();
}
}
void Scocca ()
{
if (ammo >= 0) {
audio.PlayOneShot (suonoScocca, 1);
Rigidbody bullet1 = Instantiate (Frecciadalanciare, Spawn.position, Spawn.rotation) as Rigidbody;
bullet1.AddForce (transform.forward * velocitafreccia);
} else {
audio.PlayOneShot(scarico,1);
ammo=0;
}
}
void OnGUI ()
{
GUI.Label(new Rect(1000,500,500,100),"<size=48>Munizioni:" + ammo+"</size>");
}
void OnTriggerEnter(Collider collision){
if (collision.gameObject.tag == "munizioni") {
ammo+=25;
}
}
}
the problem is the ammo packs, i tried to create a ammo pack with tag "munizioni", then i added a trigger box collider to the bullet spawn so when he triggerenters the ammo pack it increases ammo, the problem is that this doesn't work, i tried to make the ammo pack triggerable and not, but it still doesn't work!
is a rigidbody attached it to ? also change (Collider collision) to (Collider other)
Answer by jeffreyrampineda · Jan 11, 2015 at 02:23 AM
1) Assuming that this script that you have presented is attached to your typical player gameObject. Does the player gameObject have a rigidBody Component attached to it? If not, try adding a rigidBody component and setting the Collider component of that player gameObject's isTrigger to true.
2) Assuming that this script is attached to a different gameObject, try adding a rigidBody component and a Collider component with isTrigger to true to that.
3) Assuming that this script is attached to a gameObject that is a child of a parent that has a rigidBody and Collider component with isTrigger to true, try moving this script to where ever that rigidBody and Collider is.
4) Assuming that this script is attached to a gameObject that has both a rigidBody and a Collider, does the gameObject with the tag "munizioni" have a collider? If not, add one.
Basically, where ever this script is attached to, it should also have a rigidbody component and a Collider with isTrigger to true. And that whatever enters the Trigger should have a collider component attached to it as well. Tell me if it still doesn't work.
it works! THAN$$anonymous$$S! it was the 2nd one, i had to put a rigidbody and box collider with trigger to bullet spawn
Answer by SnStarr · Jan 10, 2015 at 03:07 PM
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "munizioni")
` `{
ammo+=25;
}
}
change collision.gameObject.tag to collision.name see if that works. Let me know.
do you mean a rigidbody attached to the Ammo object or Spawn_bullet object? aniway, i tried to change to collision.name, doesn't seem to work :(
Your answer
Follow this Question
Related Questions
GUI ammo counter not linked to player 0 Answers
Gun Ammo Counter - Set frame of sprite sheet 2 Answers
Gun Script not working help please 2 Answers
Please Help figure out whats wrong with Ammo Counter. 2 Answers
2D GUI Boxes/Labels not visible 1 Answer