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 /
avatar image
0
Question by Sawula · Jan 01, 2015 at 08:56 PM · c#bulletlimitnumber

Limit number of bullet on screen c#

Hey guys! I'm almost done with my project but I need a last bit of help.

I got a script for my enemies to fire at random rate, but sometime even random can fail me. I'd like a way to limit the number of bullet that can be instantiated at the same time on my screen. For example, if five of my enemies shoot at the same time, the others won't be able to shoot until those five bullets are gone. (I already have a timer for my bullets to vanish after some seconds) here is my shooting script

 using UnityEngine;
 using System.Collections;
 
 public class canonenemy : MonoBehaviour {
     public GameObject bolt;
     public Transform Canon;
     public float fireRatep;
     public float fireRatem;
     private float nextFire = 0.0f;
     // Use this for initialization
     void Start () {
         nextFire = nextFire + Random.Range (fireRatep, fireRatem);
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         if (Time.time > nextFire) {
             nextFire = Time.time + Random.Range (fireRatep, fireRatem);
         Instantiate (bolt, Canon.position, Canon.rotation);
         }
     }
 }

Not sure what can be done about it... Thanks in advance!

Comment
Add comment
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

1 Reply

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

Answer by Eluate · Jan 01, 2015 at 10:17 PM

You should have a tinker with FindGameObjectsWithTag and tags.

A solution for this question would be to assign a tag to the bolt and then using this if statement to find the amount of gameobjects with the tag in the scene.

 if (GameObject.FindGameObjectsWithTag(tag).Length < 5) {
 
     boltObject = (GameObject)Instantiate (bolt, Canon.position, Canon.rotation);
     boltObject.tag = tag;
 
 }

Note: Tags must be declared in the tag manager before using them.

Comment
Add comment · Show 2 · 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 Sawula · Jan 02, 2015 at 02:41 PM 0
Share

Unity says it doesn't know the .length I didn't use the exact code you gave because I need the double argument of fire-rate and bullet number in my 'if'.

 using UnityEngine;
 using System.Collections;
 
 public class canonenemy : $$anonymous$$onoBehaviour {
     public GameObject bolt;
     public Transform Canon;
     public float fireRatep;
     public float fireRatem;
     private float nextFire = 0.0f;
     // Use this for initialization
     void Start () {
         nextFire = nextFire + Random.Range (fireRatep, fireRatem);
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         if ((Time.time > nextFire) && (GameObject.FindWithTag("enemybolt").Length < 5)) {
             nextFire = Time.time + Random.Range (fireRatep, fireRatem);
         
         Instantiate (bolt, Canon.position, Canon.rotation);
         }
     }
 }
avatar image Eluate · Jan 02, 2015 at 09:51 PM 0
Share

You are not assigning the tag of the bullet in your code.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to limit bullets in screen like old NES game 4 Answers

Make my gun model fire bullets 1 Answer

Bullet fires before direction change 0 Answers


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