x


Effect only one gameobject when multiply have the same script

Hi guys I'm running into a bit of a jam. I have a targeting system that works like I need how ever when I send messages it gets sent to all the gameobjects with that script I've pasted the scripts below and thanks for your help in advance.

 #pragma strict

//Player Stats
static var playerhealth : int = 100;
static var Defense : float = 2;

// Base Damage
static var baseMin : int = 2;
static var baseMax : int = 5;
static var BaseDamage : int;

// Gui Display of hp
var playerHP: GUIText;
var player : GameObject;

//Crit Damage 
static var DmgMin : int = 2;
static var DmgMax : int = 2;
static var CritDamage : int;

//Crit Chance
static var CritMin : int = 1;
static var CritMax : int = 10; 
static var CritChance :int;

//Combat Variables's
var HitAmmount : int;
var playerhit : int;

//Raycast Variable's
var distance : float = 30;
var target : GameObject;

//Displays Current Target
var LockedOn : GUIText;

function Update () 
{

 if(Input.GetKeyUp(KeyCode.F)) 
     {
     Target();
     }
     if(Input.GetKeyUp(KeyCode.G)) 
     {
     AttackOne();
     }

 var txt: String = "Health Points:" +playerhealth;
 playerHP.text = txt;

 if ( playerhealth <= 0 )
 playerhealth = 0;


}
function Target () 
{
 print("locking on");


 var dir = transform.TransformDirection(Vector3.forward);
 var hit : RaycastHit;

 Physics.Raycast(transform.position, dir, hit, distance);

 print(hit.collider.gameObject);

 target = hit.collider.gameObject;

 var LockedName = hit.collider.gameObject.name;

 var txt2 : String = " Current Target" + " " +LockedName;
 LockedOn.text = txt2;
}
function AttackOne ()
{

 print("Player Atk");

 CritChance = (Random.Range(CritMin,CritMax));
 //print(CritChance + " " +"CritChance");

 if ( CritChance == 1 )
 {
 BaseDamage = (Random.Range(baseMin,baseMax));
 //print(BaseDamage + " " +"BaseDamage");

 CritDamage = (Random.Range(DmgMin,DmgMax));
 //print(CritDamage  + " " +"CritDamage");

 HitAmmount = BaseDamage * CritDamage;

 target.GetComponent(Enemy).Combat(HitAmmount);

 //print(HitAmmount);
 print("Crit");
 }
 else 
 {

 BaseDamage = (Random.Range(baseMin,baseMax));
 //print(BaseDamage + " " +"BaseDamage");

 CritDamage = (Random.Range(DmgMin,DmgMax));
 //print(CritDamage  + " " +"CritDamage");


 HitAmmount = BaseDamage;
 //print(BaseDamage + " " +"BaseDamage");
 print("No Crit");

 target.GetComponent(Enemy).Combat(HitAmmount);
 }

}

and here is the enemy script


#pragma strict

//Enemy Health
static var EnemyHealth : int = 100;
// Gui Display of hp
var EnemyHP: GUIText;

function Update()
{

 var txt: String = "Enemy Health Points:" +EnemyHealth;

 EnemyHP.text = txt;

 if ( EnemyHealth <= 0 )
 {
 EnemyHealth = 0;
 Destroy(this.gameObject);
 }

}

function Combat(dmg:int)
{
 EnemyHealth = EnemyHealth - dmg;
 print("it works");
}
more ▼

asked Jul 25 '12 at 04:32 AM

kangta gravatar image

kangta
18 5 9 12

What do mean when you "send messages" it goes to all? The F and G keys? Can you be more specific about the problem?

Jul 25 '12 at 04:52 AM Owen Reynolds

Owen thanks for your reply, however Seth has already point out my error and my code is working fine. but again thank you.

Jul 25 '12 at 04:58 AM kangta
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

Since EnemyHealth is a static var, it stays the same across all enemies. If you take out the static, I think that's what you want... So each enemy has their own health.

more ▼

answered Jul 25 '12 at 04:39 AM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

I tried that it didn't work, but thank you

Jul 25 '12 at 04:45 AM kangta

Are you sure? Is that what the problem is, that all enemies are having the same health? Try changing an enemy's health via the inspector..

Jul 25 '12 at 04:51 AM Seth Bergman

Im an idiot I was changing it in the wrong script. Thank you so much for your speedy help.

Jul 25 '12 at 04:54 AM kangta

glad to help!

Jul 25 '12 at 04:56 AM Seth Bergman
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5085
x1531

asked: Jul 25 '12 at 04:32 AM

Seen: 204 times

Last Updated: Jul 25 '12 at 04:58 AM