I basically want the weapon switching script (PlayerWeapons) to be disabled when and ONLY when reloading is happening (in the MachineGun Script) The code is right, im just getting an error:
BCE0005: Unknown identifier: ‘setEnableWeaponSwitch’.
This is my weapon switching script(PlayerWeapons)
private var b_enabled : boolean = false;
function Start () {
// Select the first weapon
SelectWeapon(0);
}
function Update () {
if (b_enabled){
// Did the user press fire?
if (Input.GetButton ("Fire1"))
BroadcastMessage("Fire");
if (Input.GetKeyDown("1")) {
SelectWeapon(0);
}
else if (Input.GetKeyDown("2")) {
SelectWeapon(1);
}
}
}
function SelectWeapon (index : int) {
for (var i=0;i<transform.childCount;i++) {
// Activate the selected weapon
if (i == index)
transform.GetChild(i).gameObject.SetActiveRecursively(true);
// Deactivate all other weapons
else
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
public function setEnableWeaponSwitch(b_enabled_fp : boolean) {
b_enabled = b_enabled_fp; //the "fp" stands for "function parameter"
}
And my MachineGun Script:
var range = 200.0;
var fireRate = 0.05;
var force = 10.0;
var damage = 5.0;
var bulletsPerClip = 100;
var clips = 5;
var reloadTime = 5;
private var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;
static var bulletsLeft : int = 0;
static var clipsLeft : int = 0;
private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;
function Start () {
hitParticles = GetComponentInChildren(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)
hitParticles.emit = false;
bulletsLeft = bulletsPerClip;
}
function LateUpdate() {
// if(Input.GetButtonDown("Reload"))
// Reload();
if (muzzleFlash) {
// We shot this frame, enable the muzzle flash
if (m_LastFrameShot == Time.frameCount) {
muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
muzzleFlash.enabled = true;
if (audio) {
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
} else {
// We didn't, disable the muzzle flash
muzzleFlash.enabled = false;
enabled = false; //<---- enable true if you want to reload manually, but with no reload time.
// Play sound
if (audio)
{
audio.loop = false;
}
}
}
}
function Fire () {
if (bulletsLeft == 0) {
///Reload();
return;
}
// If there is more than one bullet between the last and this frame, Reset the nextFireTime
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
// Keep firing until we used up the fire time
while( nextFireTime < Time.time bulletsLeft != 0) {
FireOneShot();
nextFireTime += fireRate;
}
}
function FireOneShot () {
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
// Did we hit anything?
if (Physics.Raycast (transform.position, direction, hit, range)) {
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
//decrease bullets by 1.
bulletsLeft--;
// Register that we shot this frame, so that the LateUpdate function enabled the muzzleflash renderer for one frame
m_LastFrameShot = Time.frameCount;
enabled = true;
//gun reload in time
if(bulletsLeft == 0)
Reload();
}
function Reload (){
SendMessageUpwards(setEnableWeaponSwitch, false);
//audio.PlayOneShot(reloadSound, 1.0 / audio.volume);
// Wait for reload time first - then add more bullets!
yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (clips > 0 ) {
clips--;
bulletsLeft = bulletsPerClip;
}//else {
// clips = clips;
//}
SendMessageUpwards(setEnableWeaponSwitch, true);
}
function GetBulletsLeft () {
return bulletsLeft;
}
function GetClipsLeft () {
return clips;
}
Any ideas?