I wish to clarify and amend this question.
If I am expecting to have a collision on a set layer,
there are many different types of object on that layer.
I know all of the objects will have a common variable on their script
that variables’ value is unique to them
but it is the same variable name across all different types of script
Can (and if so how) do I access that variable to read and write to ?
Can I also find out that type (the name of the script), store it as a string, then use it as a reference
e.g.
go = hit.collider.gameObject;
goScript : String = [name of go script].ToString();
goScript.health -= 1;
SendMessage is fine for one-way operations, and creating functions of variable names, to deal with those variables, but there is no way to use something like return[info]; in the script that is the target of the SendMessage.
Thanks =]
- Original Question
I am playing around making an RTS game, and it is surprising how well it is going with just giving each type of object (fighter, archer, building, ect) a behaviour, and let them run off and be happy.
However I have just hit a bump where adding different types of enemy building etc i have a steadily increasing switch-case for finding what type of enemy I am around and GetComponent of that enemy script to deduct health from.
it didn’t click when I was doing some enemies, but when I added buildings I realized this is a pain, and I have to implement a universal health system.
my first idea is to have a health script that attached to everything, that simply SendMessage to its associated gameObject to deal damage, become dead, etc.
but I would like to know If i can do it without an extra script.
Can I Find if any script on a GameObject has a specific variable name?
to put it another way :
is there a way to search for a variable on a script (or all scripts as components of a GameObject) without knowing its typecast (script name)
and possibly from that can I find the type that script is, and use it later (like currentEnemyScript.health -= 1; ) ?
Just for explanation, this is what I’m starting to notice and wish to avoid :
each enemy has a different script for their behaviour of course (a building isn’t a troop … yet!) therefore a different script name, therefore a different TypeCast :
// check for opponents
// spherecast every 0.5 secs
// then ....
for (var object : Collider in objectsInRange)
{
switch( object.gameObject.name )
{
// Troops
case "Troops_0" :
// ... Do Stuff ... then
troopTarget = object.gameObject.transform;
break;
case "Miner_0" :
// ...
minerTarget = object.gameObject.transform;
break;
case "Builder" :
// ...
builderTarget = object.gameObject.transform;
break;
// Buildings
case "Barracks" :
// ...
barracksTarget = object.gameObject.transform;
break;
case "Refinery" :
// ...
refineryTarget = object.gameObject.transform;
break;
case "0" :
break;
}
}
// store reference to targeted troop transform and script
// then follow
if ( troopTarget != null )
{
// switch case for troop name
troop0script = troopTarget.GetComponent( "CharTroops0" ) as CharTroops0;
// then follow
targetFollowing = "Troops_0";
enemyCurrentState = enemy0state.Following;
currentTarget = troopTarget;
}
else if ( minerTarget != null )
{
// switch case for troop name
miner0script = minerTarget.GetComponent( "CharMiner" ) as CharMiner;
// then follow
targetFollowing = "Miner_0";
enemyCurrentState = enemy0state.Following;
currentTarget = minerTarget;
}
else if ( builderTarget != null )
{
etc
Edit :
My idea of a health class that is applied to all Objects that can be damaged
the health script :
// the main script finds that var and changes it at start
// e.g. if a fighter then health = 5; if an archer then health = 3; etc
var health : float = 5.0;
function JustBitTheDust()
{
// disable the troop gameObject
// add the troop back to the pool
}
on the troop :
// when finding a target
targetHealthScript = hit.collider.gameobject.GetComponent("GenericHealthScript") as GenericHealthScript;
// every cycle that the troop does damage
targetHealthScript.health -= 1;
if (health <= 0) targetHealthScript.JustBitTheDust();