- Home /
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;
}
}
}
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! =]
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.
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?
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 ()"
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
Follow this Question
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