Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 wordsstuff · Mar 07, 2017 at 04:22 PM · shootinglimitlimitationslimitslimitation

How To Add A Limit To Amount Of Objects

HELLO! It is me, I am working on a new project and in it the player has to place down bricks to build castles, and I cant figure out how to add a limit as to how many bricks the player can put down in a level, I have a "shooting" script, and when you press the left mouse button then the chosen projectile spawn area will spawn a projectile (brick) and in this game it will drop it then the player uses that to build their castle, what I want to do is add some sort of constraint to the number of bricks the player can place down, so they don't mess the game up and to make it more challenging. Any ideas, answers, or questions that may help me with this problem? My shooting script is bellow, if you need any other information that will help you please comment and I will respond with the answer, I really need to figure out how to do this, THANKS!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class Shoot : MonoBehaviour {
     public Rigidbody2D projectile;
     public Transform projectileSpawnPoint;
     public float projectileVelocity;
     public float timeBetweenShots;
     private float timeBetweenShotsCounter;
     private bool canShoot;
     // Use this for initialization
     void Start () {
         canShoot = false;
         timeBetweenShotsCounter = timeBetweenShots;
     }
 
     // Update is called once per frame
     void Update () {
         if (Input.GetMouseButtonDown(0) && canShoot)
         {
             Rigidbody2D bulletInstance = Instantiate(projectile, projectileSpawnPoint.position, Quaternion.Euler(new Vector3(0, 0, transform.localEulerAngles.z))) as Rigidbody2D;
             bulletInstance.GetComponent<Rigidbody2D>().AddForce(projectileSpawnPoint.right * projectileVelocity);
             canShoot = false;
         }
         if (!canShoot)
         {
             timeBetweenShotsCounter -= Time.deltaTime;
             if(timeBetweenShotsCounter <= 0)
             {
                 canShoot = true;
                 timeBetweenShotsCounter = timeBetweenShots;
             }
         }
     }
 
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 Bryangs · Mar 07, 2017 at 05:36 PM

There is a very easy way to do it. Here is how it will basically work: There will be a list in your script. It will detect any brick that is on the current scene. If there are X bricks, then the player is not going to be able to shoot anymore.

Now let's get to the coding part. First, you will need an array. Arrays are basically lists you can use to hold more than one thing without making a giant code, and it has a lot of other cool functions. You will also need an Int, we will call it number. You will have to change number (the int)'s value to the one you want to be the maximum bricks the player can spawn.

 private GameObject[] brickList;
 public int number;

After making an Array, go to the Editor again, and on the Inspector, create a new tag and tag the original brick prefab as it. Let's say you made a new tag called BRICK and you tagged your brick prefab as said tag.

Now we need to tell the array to store all the GameObjects tagged as BRICK into it. I am going to use it on the Update function, but there are a lot of better ways to do it, this is just an example.

     Void Update()
 {
     brickList = GameObject.FindGameObjectsWithTag("BRICK");
 }

Now we just need to make sure that IF our Array does have X number of bricks, the player can't shoot anymore (We will disable his canShoot bool).

             if (brickList[number])
             {
                 canShoot = false;
             }

As i said, this is just an Example. There are a lot of other ways to do it, i just wanted to make it as simple as possible so you could easily understand. I hope this helped, Good Luck! =]

Comment
Add comment · Show 4 · 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 Bryangs · Mar 07, 2017 at 05:39 PM 1
Share

You can also ins$$anonymous$$d of using the brickList = GameObject.FindGameObjectsWithTag("BRIC$$anonymous$$");

on the Update function, you could make a normal function, and call it everytime you spawned a brick. This would consume a lot less power from your pc.

avatar image wordsstuff · Mar 07, 2017 at 09:42 PM 0
Share

So does it matter where I put the update function code in the update function or is there a specific place that I need to put it?

avatar image wordsstuff · Mar 07, 2017 at 10:06 PM 0
Share

GREAT! It works there is just one problem do you think you could help me fix it, the problem is that I get an error in unity saying "IndexOutOfRangeException: Array index is out of range. Shoot.Update ()"

avatar image Bryangs wordsstuff · Mar 07, 2017 at 11:26 PM 0
Share

Oh, that's because you are probally missing a basic rule with the Arrays.

They don't work in this order: 1,2,3,4,5... They work in this order: 0,1,2,3,4...

So if your $$anonymous$$ax amount of bricks is for an example 50, you should write 49. That's because the first number in an Array is actually the Element 0.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Shooting with Limited Ammo 2 Answers

Slope Limitation 2 Answers

Minimum value in public variables? 4 Answers

HingeJoint Does Not Revert to Correct Limits 1 Answer

How do I limit the speed of my car? 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