- Home /
Question by
sinkler747 · Mar 09, 2013 at 07:46 PM ·
variableaccessing
when i access a variable from another script, it doesn't change the variable in that script. what am i doing wrong?
what i am trying to do, is when a enemy gets hit, it gives him a random moveSpeed value. so i accessed the variable moveSpeed from the enemyAI script. when i run the game and i shoot an enemy, it does register the change as far as the debug.Log says, but when i watch the script variables in the inspector for the enemyAI script, they are not changing. what am i doing wrong?
thanks
#pragma strict
var loweredGun: Transform; // assign to gunMount empty game object
var raisedGun: Transform; // aimedGun to aimGun empty game object
var rayOrigin: Transform;
var gun_01: GameObject;
var gun_02: GameObject;
var myScriptMoveSpeed: enemyAI; // this is the script i am accessing, in particular the var moveSpeed variable
var shootAudio: AudioClip;
var reloadAudio: AudioClip;
var rifleShootAudio: AudioClip;
var range: float = 1000;
var force: float = 1000;
var clip : int = 3;
var bulletsPerClip:int = 6;
var reloadTime :float = 3.3;
var bulletsLeft: int = 0;
var shootTimer: float = 0;
var shootCooler: float = 0.9;
var playerHealth: int = 100;
var damage: int = 10;
var reset: boolean = false;
//var enemyAIScript: enemyAI;
private var randRange: int;
private var randMoveSpeed: float; // this randomizes the moveSpeed between to float values
function Start ()
{
bulletsLeft = bulletsPerClip;
gun_01.active = true;
myScriptMoveSpeed = GameObject.FindGameObjectWithTag("Zombie").gameObject.GetComponent(enemyAI);
//enemyAIScript = gameObject.Tag("enemy");
}
function Update ()
{
if(shootTimer > 0)
{
shootTimer -= Time.deltaTime;
}
if(shootTimer < 0)
{
shootTimer = 0;
}
if(Input.GetKeyDown(KeyCode.X))
{
SwitchWeapons();
}
if(Input.GetMouseButton(0) && bulletsLeft)
{
if(shootTimer == 0)
{
PlayShootAudio();
RayShoot();
shootTimer = shootCooler;
}
}
}
function RayShoot()
{
var hit: RaycastHit;
var directionRay = rayOrigin.TransformDirection(Vector3.forward); // make sure rayOrigin is set with a game object that will point the firing line.
Debug.DrawRay(rayOrigin.position, directionRay * range, Color.red);
if(Physics.Raycast(transform.position, directionRay, hit, range))
{
if(hit.collider.gameObject.name == GameObject.FindWithTag("Body").gameObject.name)
{
Debug.Log("I hit the body");
//animation.Play("hit1");
randRange = Random.Range(1,4);
randMoveSpeed = Random.Range(0.01,1.0);
Debug.Log(randRange);
if(randRange == 1)
{
hit.rigidbody.animation.Play("hit1");//(Random.value(hit1, hit2));//(directionRay * force, hit.point);
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
myScriptMoveSpeed.moveSpeed = 0;
Debug.Log("movement is: ");
Debug.Log(myScriptMoveSpeed.moveSpeed);
yield WaitForSeconds(.3);
hit.rigidbody.animation.Play("shamble");
//GameObject.FindWithTag("Zombie").gameObject.GetComponent(enemyAI.moveSpeed);
myScriptMoveSpeed.moveSpeed = randMoveSpeed;
//myScriptMoveSpeed = GameObject.FindGameObjectWithTag("Zombie").gameObject.GetComponent(enemyAI);
Debug.Log("movement is now: ");
Debug.Log(myScriptMoveSpeed.moveSpeed);
}
else if(randRange == 2)
{
hit.rigidbody.animation.Play("hit2");//(Random.value(hit1, hit2));//(directionRay * force, hit.point);
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
myScriptMoveSpeed.moveSpeed = 0;
Debug.Log("movement is: ");
Debug.Log(myScriptMoveSpeed.moveSpeed);
yield WaitForSeconds(.3);
hit.rigidbody.animation.Play("shamble");
myScriptMoveSpeed.moveSpeed = randMoveSpeed;
Debug.Log("movement is now: ");
Debug.Log(myScriptMoveSpeed.moveSpeed);
}
else if(randRange == 3)// this is the fall animation
{
hit.rigidbody.animation.Play("fallBack");//(Random.value(hit1, hit2));//(directionRay * force, hit.point);
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
yield WaitForSeconds(.5);
myScriptMoveSpeed.moveSpeed = 0;
Debug.Log("movement is: ");
Debug.Log(myScriptMoveSpeed.moveSpeed);
yield WaitForSeconds(.5);
Debug.Log("now he stands up");
hit.rigidbody.animation.Play("standUp");
yield WaitForSeconds(.5);
myScriptMoveSpeed.moveSpeed = randMoveSpeed;
hit.rigidbody.animation.Play("shamble");
Debug.Log("movement is: ");
Debug.Log(myScriptMoveSpeed.moveSpeed);
}
}
else if(hit.collider.gameObject.name == GameObject.FindWithTag("HeadCollider").gameObject.name)
{
Debug.Log("That was a headshot");
hit.rigidbody.animation.Play("fallBack");
yield WaitForSeconds(1);
hit.collider.SendMessageUpwards("KillParent",SendMessageOptions.DontRequireReceiver);
}
}
bulletsLeft --;
if(bulletsLeft <= 0)
{
bulletsLeft = 0;
clip --;
}
if(bulletsLeft == 0)
{
Reload();
}
}
function Reload()
{
PlayReloadAudio();
yield WaitForSeconds(reloadTime);
if(clip >0)
{
bulletsLeft = bulletsPerClip;
}
}
function PlayShootAudio()
{
if(gun_01.active)
{
audio.PlayOneShot(shootAudio);
}
if(gun_02.active)
{
audio.PlayOneShot(rifleShootAudio);
}
}
function PlayReloadAudio()
{
audio.PlayOneShot(reloadAudio);
}
function SwitchWeapons()
{
if (gun_01.active == true)
{
gun_01.SetActiveRecursively(false);
gun_02.SetActiveRecursively(true);
}
else
{
gun_01.SetActiveRecursively(true);
gun_02.SetActiveRecursively(false);
}
}
Comment