- Home /
Pickup Bullets
Hi, i am making a shooting game where there are bullets scattered on the ground. the first person controller has to pick them up and shoot the bird. I cant seem to pick up the bullets. Also, the first person controller passes right through the bullets instead of picking em up. I am pasting my code here. Someone please help ASAP :s Bullet.cs: using UnityEngine; using System.Collections; public class Bullet : MonoBehaviour { public GameObject bullet; public static int count = 0; //public AudioClip beep; // Use this for initialization void Start () { count = 0; } // Update is called once per frame void Update () { } void Pickup() { count++; } void OnTriggerEnter (Collider col) { if(collider.gameObject.tag == "Player") { //AudioSource.PlayClipAtPoint(beep, transform.position); collider.gameObject.SendMessage ("Pickup"); Destroy(gameObject); } } }
Inventory.cs enter code hereusing UnityEngine; using System.Collections;
public class Inventory : MonoBehaviour {
public static int count = 0;
public AudioClip beep;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void Pickup()
{
AudioSource.PlayClipAtPoint (beep, transform.position); //will play sound at a specific position
count++;
}
}
Answer by sami1592 · Mar 16, 2014 at 07:16 PM
when ever the player collides with the bullet you want to call the function PickUp() in the Inventory.cs right?
when ever a collision occurs with the player, the pickup function of the collided object is called. So in order this to work you have to attach Inventory.cs to the player. My guess is you haven't don't it, so the pickUp function is called in the Player's scripts bt it cant find any function named PickUp in the controllers script
let us know if the problem is solved
oh one other thing, in the first script there will be a correction
void OnTriggerEnter (Collider col)
{
// here
if(col.gameObject.tag == "Player")
{
//AudioSource.PlayClipAtPoint(beep, transform.position);
collider.gameObject.SendMessage ("Pickup");
Destroy(gameObject);
}
}
Attaching the Script to the player
look at the code, in line 3-4