- Home /
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");
}
What do mean when you "send messages" it goes to all? The F and G keys? Can you be more specific about the problem?
Owen thanks for your reply, however Seth has already point out my error and my code is working fine. but again thank you.
Answer by Seth-Bergman · Jul 25, 2012 at 04:39 AM
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.
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..
Im an idiot I was changing it in the wrong script. Thank you so much for your speedy help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074649im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Disabling a lineRender or Ray when there is no target 1 Answer
racing game raycast problem 2 Answers
Raycasting 1 Answer