I have a public variable called powerUnits that I wish to be used across a variety of scripts. For the particular script I am writing, I want to check the availability of the variable then reduce it when the space bar is pressed. This should then have it fire 10 bullets.
The problem is that nothing happens at all. There is no firing nor depletion of units. What is more puzzling is that no errors are shown in the console. I have assigned what object to instantiate as well as the object to take location and orientation from. The script was modified from a previous script that I wrote that did not take powerUnits into account, and that worked without any problems.
I’m new to programming, so I greatly appreciate the help. If possible, please help me in terms of C#, I’m still learning the language, and I don’t feel too ready to add learning java to the equation. Maybe in a month or two…
Here is my problem script:
using UnityEngine;
using System.Collections;
public class Bullet1Fire : MonoBehaviour {
public Rigidbody bullet;
public Transform barrel1End;
private int wait = 25;
TransformFunctions transformFunctions;
void Awake(){
transformFunctions = GetComponent<TransformFunctions>();
}
// Update is called once per frame
void Update () {
//Check powerUnit count from TransformFunctions script
if(transformFunctions.powerUnits >= 100){
//check for spacebar down
if(Input.GetKeyDown(KeyCode.Space)){
//lower powerUnits
transformFunctions.powerUnits -= 100;
//Fire ten bullets
while(wait>=0){
Rigidbody bulletInstance;
bulletInstance = Instantiate(bullet, barrel1End.position, barrel1End.rotation) as Rigidbody;
bulletInstance.AddForce(barrel1End.forward * 5000);
wait --;
}
}
}
}
}
Here is a renamed version of the original working code:
using UnityEngine;
using System.Collections;
public class Bullet2Fire : MonoBehaviour {
public Rigidbody bullet;
public Transform barrel1End;
private int wait = 50;
TransformFunctions transformFunctions;
// Update is called once per frame
void Update () {
if(wait == 0){
if(Input.GetKey(KeyCode.Space)){
Rigidbody bulletInstance;
bulletInstance = Instantiate(bullet, barrel1End.position, barrel1End.rotation) as Rigidbody;
bulletInstance.AddForce(barrel1End.forward * 5000);
wait --;
}
}
}
}
Thanks in advance, and I’m sorry for any sloppy techniques!