- Home /
Calling variable accross scripts in C# problem (Closed)
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!
Answer by Fattie · Jun 09, 2013 at 06:02 AM
You can very easily solve your problem by adding
Debug.Log();
statements.
void Awake() { transformFunctions = GetComponent(); transformFunctions.test(); }
You did not show us your file TransformFunctions but add this function to it ..
void Test() { Debug.Log("I am in 'test' "); }
Please be sure to TICK any useful answer, thanks.
Your answer
Follow this Question
Related Questions
Using a variable value with GetComponent 1 Answer
fix script to get C# var from another object instead of object this script is attached to 2 Answers
C# Access to a variable by another script attached in a gameObject in the scene. 2 Answers
Syntax troubles (GetComponent and changing variables) 1 Answer
Getting a Variable on Script B to equal a variable from script A 0 Answers