- Home /
Accessing Script that Raycast hit
Hello, I've been at this for hours. I've got a level with a bunch of zombies placed from a prefab. They run around and try and kill the player.
The player has a weapon that casts a ray when mouse is down, and if it hits a zombie, it is meant to access that zombies health script and change its life value.
The problem I'm having, is when the ray hits any zombie, all zombies take damage. I'm not sure where I'm going wrong. This is my weapon script:
#pragma strict
private var fireRate : float;
private var nextFire : float;
var hit : RaycastHit;
var weaponDamage : float;
//Weapon sound effects
var shotgunFire : AudioClip;
function Start()
{
//Set rate of fire
fireRate = 2;
weaponDamage = 1;
}
function Update()
{
if (Input.GetButton ("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
audio.PlayOneShot(shotgunFire);
//Cast ray, if hit zombie, deal damage to selected zombie
if (Physics.Raycast (transform.position, transform.forward, hit, 100))
{
//This doesn't draw a line, I mustn't be using it correctly
Debug.DrawLine (transform.position, transform.forward);
if (hit.collider.gameObject.tag == "zombie")
{
var enemyLifeScript = hit.collider.GetComponent(ZombieHealth);
if (enemyLifeScript)
enemyLifeScript.life -= weaponDamage;
//Other Things I've tried:
//hit.collider.gameObject.GetComponent(ZombieHealth).life = hit.collider.gameObject.GetComponent(ZombieHealth).life - weaponDamage;
//ZombieHealth.life = ZombieHealth.life - weaponDamage;
}
}
}
}
@script RequireComponent(AudioSource)
First thing that came to $$anonymous$$d: ZombieHealth.life isn't somehow a static/global variable is it?
How are you visualizing/debugging the health value?
Hi Jozxyqk, the life variable is a static variable, and it is modified from outside the ZombieHealth Script.
What do you mean by visualizing / debugging the health value?
do not use static variables. check unityGE$$anonymous$$S.com for a basic intro to accessing variables from other scripts
A static field identifies exactly one storage location to be shared by all instances of a given closed class type. Hejlsberg et al., p. 434
Answer by LukaKotar · Nov 24, 2012 at 09:28 AM
Basically, how I would do this would be by placing a function inside of your zombie script that would inflict the damage, and the damage would be handled by a float parameter.
In your zombie health script, you can add something like this:
function ApplyDamage (damage : float) {
life -= damage;
}
And to do damage you would call the function on the hit object, and tell it how much damage to inflict:
if(hit.collider.tag == "zombie"){
hit.gameObject.SendMessageUpwards("ApplyDamage", weaponDamage); //Tell the game object you've hit to initiate a function called "ApplyDamage", and set the 'damage' float parameter to a desired value, which would be weaponDamage
}
Hope it works.
Hi thanks for your help, just so I can get some better understanding of all of this. I've tried reading up on "Send$$anonymous$$essageUpwards", but I don't fully get it. Could you explain what that term means?
Well, the Send$$anonymous$$essageUpwards() function basically calls a function on another object. First thing you type in it is a function name. So for example, if you would have a function called Example(), and would like to call it from another object, you could use Send$$anonymous$$essageUpwards("Example") to call the function. If it has at least one parameter, you'd have to tell them their values. For example, Example (parameter : float) is a function that contains a parameter that I named "parameter". If you do have any, you will need to set the values. To set the value upon calling the function with Send$$anonymous$$essageUpwards, you would as before, put the function name as string in the beginning, and then just put values of the same type the parameter is. Example: Send$$anonymous$$essageUpwards("Example", 2.5) . You can also set the values from variables.
Ahhhh thats pretty awesome thank you. I've set it up and it all looks good, but I'm gettin an error: 'gameObject' is not a member of unity bla bla. this is what I wrote:
if(hit.collider.tag == "zombie") { hit.gameObject.Send$$anonymous$$essageUpwards("ApplyDamage", weaponDamage); }
Oops, try 'transform' ins$$anonymous$$d of 'gameObject'.
hit.transform.Send$$anonymous$$essageUpwards("ApplyDamage", weaponDamage);
What happens if there are two scripts with two functions both named the same and both called by "Send$$anonymous$$essageUpwards"?
Answer by IT_Criminal · Nov 24, 2012 at 01:06 PM
RaycastHit hit;
if(Physics.Raycast(CurrentWeapon.BarrelGO.position,CurrentWeapon.BarrelGO.TransformDirection(Vector3.forward),hit,250.0))
{
hit.transform.SendMessage("ApplyDAmage",CurrentWeapon.damage);
}
i use this ...
replace currentweapon with the class that you are using for your weapons and barrelGO with the bullet spawnpoint (make sure the bullet-spawnpoint gameobject is pointing with the blue arrow torwards the direction of the shooting)