I am trying to get my Assault Rifle script to pick up the damage from the Apply Damage Script. However, this is my error in Unity.
BCE0019: ‘other’ is not a member of ‘Object’
#pragma strict
var damage = 2;
// instant kill
function OnCollisionEnter(collisionInfo)
{
collisionInfo.other.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
//Attach to Bullet
This is my Assault Rifle Script.
var range = 100.0; //This is just a default value to give us a range that our weapon can shoot
var fireRate = 1.0; //This is another default value that we can change directly in the editor to change the rate of fire
var force = 10.0; //This variable gives us the ability to adjust how much force our weapon has when it shoots something
var damage = 10.0; //This allows us to apply damage to enemies if they have hit points that must reach 0 before they actually die
var bulletsPerClip = 15; //This variable gives us flexibility to assign how many bullets go into a clip of a specific weapon
var clips = 10; //This variable gives the ability to have limited ammo
var reloadTime = 0.5; //We need to be able to adjust how long it takes to reload our weapon
private var hitParticles : ParticleEmitter; //We need some visual feedback that our bullets are hitting something
var muzzleFlash : Renderer; //We also need to see if it's actually firing, plus it looks cool
private var bulletsLeft : int = 0; //This variable is going to store how many bullets we have left in our clip
private var nextFireTime = 0.0; //This is going to regulate our fire rate to use actual time instead of how fast the computer runs
private var m_LastFrameShot = -1; //This also helps regulate the fire rate
var rifleAmmoGUI : DrawAmmo;
function Start () //Start functions run any code as the level starts but does not continue to update
{
//Add Observer
NotificationCenter.DefaultCenter().AddObserver(this, "Fire");
NotificationCenter.DefaultCenter().AddObserver(this, "Reload");
//Check if we have an Ammo GUI set, if not, find one
if(!rifleAmmoGUI)
{
rifleAmmoGUI = GameObject.Find("Ammo").GetComponent(DrawAmmo);
}
//Get the particle system attached to the AssaultRifleGO
hitParticles = GetComponentInChildren(ParticleEmitter);
//Keeps particles from emitting all the time
if(hitParticles)
{
hitParticles.emit = false;
bulletsLeft = bulletsPerClip;
}
}
function LateUpdate() //LateUpdate functions updates every frame as long as the behaviour is enabled
{
UpdateGUI();
if(muzzleFlash)
{
//We shot this frame so enable the muzzle flash
if(m_LastFrameShot == Time.frameCount)
{
//Enable our muzzle flash and animate it to rotate
muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.Range(0, 359), Vector3.forward);
muzzleFlash.enabled = true;
//Play sound
if(audio)
{
if(!audio.isPlaying)
{
audio.Play();
audio.loop = true;
}
}
}
//We need to disable the muzzle flash
else
{
muzzleFlash.enabled = false;
enabled = false;
//Stop sound
if(audio)
{
audio.loop = false;
}
}
}
}
//Can we fire? This function is going to check
function Fire ()
{
if(bulletsLeft == 0)
{
return;
}
//If there is more than one bullet between the last and this frame, reset the nextFireTime
//This help regulate the fire rate to actual time
if(Time.time - fireRate > nextFireTime)
{
nextFireTime = Time.time - Time.deltaTime;
}
//Keep firing until we have used up the fire time
while(nextFireTime < Time.time bulletsLeft != 0)
{
FireOneShot();
nextFireTime += fireRate;
}
}
var layerMask = 3328;
layerMask = ~layerMask;
//This function tells our weapon how to shoot
function FireOneShot()
{
//We need to cast a ray out in front of the player
var direction = transform.TransformDirection(Vector3.forward);
var cam : Transform = Camera.main.transform;
var hit : RaycastHit;
//Check to see if we hit anything
if(Physics.Raycast(cam.position, cam.forward, hit, range, layerMask))
{
//Apply force to the rigid body we hit
if(hit.rigidbody)
{
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
}
//Spawn particles at the point we hit the surface
if(hitParticles)
{
var newParticles = Instantiate(hitParticles, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
newParticles.GetComponent(ParticleAnimator).autodestruct = true;
}
//Send damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
bulletsLeft --;
//We need to tell the LateUpdate function that we shot and that it can enable the muzzle flash and audio
m_LastFrameShot = Time.frameCount;
enabled = true;
//We need to reload automatically if the clip is empty
if(bulletsLeft == 0)
{
Reload();
}
}
function Reload()
{
Debug.Log("We are loading our weapons.");
//We want the actual reload to wait while the animation plays
yield WaitForSeconds(reloadTime);
//We need to check if we have a clip to reload
if(clips > 0)
{
clips--;
bulletsLeft = bulletsPerClip;
}
}
//We may need to check how many bullets to reload if we have total ammo count
function GetBulletsLeft()
{
return bulletsLeft;
}
function UpdateGUI()
{
if(this)
{
rifleAmmoGUI.UpdateAmmo(bulletsLeft);
}
}
This is my Energy Script
var maximumHitPoints = 100.0;
var hitPoints = 100.0;
//var bulletGUI : GUIText;
//~ var rocketGUI : GUITexture;
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 rocketLauncher : RocketLauncher;
private var healthGUIWidth = 0.0;
private var gotHitTimer = -1.0;
var rocketTextures : Texture[];
function Awake () {
machineGun = GetComponentInChildren(MachineGun);
//~ rocketLauncher = GetComponentInChildren(RocketLauncher);
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, 0.5);
}
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 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) {
//~ 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];
//~ }
//~ }
}