- Home /
Why is this player script not working
OK, it seems there is ALWAYS something wrong. I get an error message at line 19,31 but it looks straight forward enough to me, it follows along with the rest of the weapons attacked to my FPS main camera so I can't figure out why i'm getting this error??
Here is my Script:
var maximumHitPoints = 100.0; var hitPoints = 100.0;
var grenadesGUI : GUIText; var bulletGUI : GUIText; var shotgunGUI : GUIText; var rocketGUI : DrawRockets; var healthGUI : GUITexture;
var walkSounds : AudioClip[]; var painLittle : AudioClip; var painBig : AudioClip; var die : AudioClip; var audioStepLength = 0.3;
private var machineGun : MachineGun; private var shotgun : Shotgun; private var rocketLauncher : RocketLauncher; private var grenadeLauncher : GrenadeLauncher; private var healthGUIWidth = 0.0; private var gotHitTimer = -1.0;
var rocketTextures : Texture[];
function Awake () { shotgun = GetComponentInChildren(Shotgun); machineGun = GetComponentInChildren(MachineGun); rocketLauncher = GetComponentInChildren(RocketLauncher); grenadeLauncher = GetComponentInChildren(GrenadeLauncher);
PlayStepSounds();
healthGUIWidth = healthGUI.pixelInset.width;
}
function ApplyDamage (damage : float) { if (hitPoints < 0.0) return;
// Apply damage
hitPoints -= damage;
// Play pain sound when getting hit - but don't play so often
if (Time.time > gotHitTimer && painBig && painLittle) {
// Play a big pain sound
if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
audio.PlayOneShot(painBig, 1.0 / audio.volume);
gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
} else {
// Play a small pain sound
audio.PlayOneShot(painLittle, 1.0 / audio.volume);
gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);
}
}
// Are we dead?
if (hitPoints < 0.0)
Die();
}
function Die () { if (die) AudioSource.PlayClipAtPoint(die, transform.position);
// Disable all script behaviours (Essentially deactivating player control)
var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
for (var b in coms) {
var p : MonoBehaviour = b as MonoBehaviour;
if (p)
p.enabled = false;
}
LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 2.0);
}
function LateUpdate () { // Update gui every frame // We do this in late update to make sure machine guns etc. were already executed UpdateGUI(); }
function PlayStepSounds () { var controller : CharacterController = GetComponent(CharacterController);
while (true) {
if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
audio.Play();
yield WaitForSeconds(audioStepLength);
} else {
yield;
}
}
}
function UpdateGUI () { // Update health gui // The health gui is rendered using a overlay texture which is scaled down based on health // - Calculate fraction of how much health we have left (0...1) var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
// - Adjust maximum pixel inset based on it
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
// Update machine gun gui
// Machine gun gui is simply drawn with a bullet counter text
if (machineGun) {
bulletGUI.text = machineGun.GetBulletsLeft().ToString();
}
// Update Shotgun gui
// Shotgun gui is simply drawn with a bullet counter text
if (shotgun) {
shotgunGUI.text = shotgun.GetBulletsLeft().ToString();
}
// Update Grenade Launcher gui
// Grenade Launcher gui is simply drawn with a bullet counter text
if (grenadeLauncher) {
grenadesGUI.text = grenadeLauncher.GetBulletsLeft().ToString();
}
// Update rocket gui
// This is changed from the tutorial PDF. You need to assign the 20 Rocket textures found in the GUI/Rockets folder
// to the RocketTextures property.
if (rocketLauncher) {
rocketGUI.UpdateRockets(rocketLauncher.ammoCount);
/*if (rocketTextures.Length == 0) {
Debug.LogError ("The tutorial was changed with Unity 2.0 - You need to assign the 20 Rocket textures found in the GUI/Rockets folder to the RocketTextures property.");
} else {
rocketGUI.texture = rocketTextures[rocketLauncher.ammoCount];
}*/
}
}
EDIT: this is the error message I get Assets/WeaponScripts/FPSPlayer.js(121,52): BCE0019: 'GetBulletsLeft' is not a member of 'MissileLauncher'.
I should also mention that I changed my Grenade Launcher to "Missile Launcher" since I thought maybe it was calling on that script. I noticed the scripts attached to all of the other weapons had the same name so just to be consistant I changed the name to match... lol it didn't help though :(
Here is my Missile Launcher Script:
var projectile : Rigidbody;
var initialSpeed = 20.0;
var reloadTime = 0.6;
var ammoCount = 20;
private var lastShot = -10.0;
function Fire ()
{
// Did the time exceed the reload time?
if (Time.time > reloadTime + lastShot && ammoCount > 0)
{
// create a new projectile, use the same position and rotation as the Launcher.
var instantiatedProjectile : Rigidbody = Instantiate (projectile,
transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of
// the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(
Vector3 (0, 0, initialSpeed));
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
ammoCount--;
}
}
Say what the error is, and point out exactly what line it's on.
I updated my Post with the error message I get and I added my "$$anonymous$$issile Launcher" Script.
Answer by DaveA · Mar 19, 2011 at 02:58 AM
GetBulletsLeft() is indeed not a function in your MissileLauncher script. I don't know how your other weapons scripts look, but they either have such a function, or are subclassed from a class that has it. I'd then look at the other scripts to see how they define GetBulletsLeft and add that to your missile launcher script, or something along that line. Or subclass missile launcher from whatever your other weapons subclass from (if anything).
$$anonymous$$y other scripts for the machine bun and shotgun are just raycast bullets but my $$anonymous$$issile (Grenade launcher) has a ridged body projectile so that could be a big difference but I'm not sure how to make that work and update the GUI text in my players GUI?? Any thoughts on how this is done, I've been toying around with this one for quite some time. I'm also still trying to figure out how to make weapons pickup items.. so much I'm still trying to figure out and not enough indeapth tutorials out there. $$anonymous$$ost of the ones I come accross use boxes and cubes to chase characters around :(
Your answer
Follow this Question
Related Questions
GUI Scale Problem 1 Answer
[Closed] MainMenu Script error 2 Answers
FPS weapon sway script error 0 Answers
Error BCE0051: Operater '==' cannot be used. Please Help! 1 Answer
what is wrong with this online FPS script(not done) ? 1 Answer