Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Davide-Barbieri · Jan 10, 2015 at 01:06 PM · counterammo

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hypnoticmeteor · Jan 10, 2015 at 01:10 PM 0
Share

is a rigidbody attached it to ? also change (Collider collision) to (Collider other)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Davide-Barbieri · Jan 11, 2015 at 09:01 AM 0
Share

it works! THAN$$anonymous$$S! it was the 2nd one, i had to put a rigidbody and box collider with trigger to bullet spawn

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Davide-Barbieri · Jan 11, 2015 at 01:26 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges