- Home /
All Three Game Objects Fire At Once
Hey I have three game objects/Bullet Factories that fire all at the same time when i press spacebar instead of them fire individually when selected any ideas ? here is my script
#pragma strict
var bullet : Rigidbody;
function Start ()
{
}
function Update ()
{
if(Input.GetKeyDown("space"))
{
//everything inside brackets runs
var latestBullet = Instantiate(bullet, transform.position, transform.rotation);
latestBullet.AddRelativeForce (0, 0, 500);
}
}
What are you expecting to happen? There's no code inside of this that says "only fire one" or "only fire one based on how many times I've already fired". Do you want one to fire at a time, or do you want them to fire on the same button click, but one after the other?
Well I have three game objects attached to three cameras and the idea is when i press space bar the game object fires pending on which ever camera it is attached to. It doesnt have a bullet count or anything but i would want it to only fire once for every time space bar is pressed down
Answer by aldonaletto · Oct 27, 2013 at 02:38 PM
You must enable the shooting code only when the object is selected. Supposing that this script has a public boolean variable selected that is true when the object is selected, the code can be like this:
#pragma strict
public var bullet : Rigidbody;
public var selected : boolean = false; // set this to true when object selected
function Update (){
if (selected && Input.GetKeyDown("space")){
var latestBullet = Instantiate(bullet, transform.position, transform.rotation);
latestBullet.AddRelativeForce (0, 0, 500);
}
}
Just tried that script and it does work. Im bad a getting my point across sorry I have a feature on another script where if a right click is pressed then the player can switch camera but can only see through one camera at a time. So I was meaning if the player switches to camera 2 with another first person controller attached to it can it automatically turn camera 1 and camera 3s shooter off unless a right click is made which will switch to camera 3 ?
It depends on how you're selecting the cameras. If you're enabling the selected camera while disabling the others, you could set the variable selected like this:
function Update(){
// selected is true when the parent camera is enabled:
selected = transform.parent.camera.enabled;
if (selected && Input.Get$$anonymous$$eyDown("space")){
...
Another possibility would be to activate the selected camera and deactivate the others in the camera switching script with SetActive() - this would not require any additional code to stop the shooters, since a deactivated object also deactivates all its children (what includes the weapon). I don't know how your camera switching script is, but that's a suggestion:
public var cameras: Camera[]; // drag the cameras here
private var curCam = 0;
function Update(){
if (Input.Get$$anonymous$$ouseButtonDown(1)){ // if right button pressed...
curCam = (curCam+1)%3; // cycle to the next camera
// activate only cameras[curCam], deactivate the others
for (var i=0; i<cameras.Length; i++){
cameras[i].gameObject.SetActive(i == curCam);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613120719im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
ContactPoint with OnParticleCollision? 0 Answers
Rapid fire Help 1 Answer
need help with instantiating for making a bullet shoot 1 Answer
How to Sync bullet spawn with shoot animation 0 Answers
Enemy Damage problem 1 Answer