Is it possible to use a String to represent the name of a script in GetComponent()

I’m at a point where I’m seriously considering going back and creating my own classes for the game I’m making, but if is at all possible I’d rather avoid it. Let’s say I have TurretA, TurretB, TurretC, TurretLaser, TurretElephant, that all have a unique script each, that matches their name. This is all done in javascript.

Let’s also say that because I have no classes, some of these scripts have common variables, let’s use Damage as an example.

Manual GetComponents are easy, I’m fine with that but let’s say I have a function that wants to access some of these generic variables. I have a “selected” turret, via clicking on it, in my UI frame I would like to display the damage of this selected turret.

So what I’m looking for is something like ScriptString =, followed by a GetComponent() that passes ScriptString as the script name (earlier I stated that all turret scripts are identical to the turrets name).

This way I wouldn’t have to create unique cases/if’s relevant to the individual turret type, it would see it’s name, build the script string, pass the string into the GetComponent, I could pull the damage value out of the script and display it where I want.

Is this possible? Or am I going to need to go back and build myself some classes, and use genuine inheritance.

Using GetComponent(“ScriptName”) doesn’t allow access to the script variables because the compiler doesn’t know the class. You could instead use SendMessage to call specific functions by name in the target script (this is widely used in the FPS Tutorial to apply damage and to fire weapons) and modify the variables in these functions. If you just want to apply damage, write a function ApplyDamage in each turret script to do the job - like this (turret script):

var health: float = 100;

function ApplyDamage(damage: float){
  health -= damage;
  if (health <= 0){
    // turret has died!

In the caller script, call SendMessage prefixed by a reference to the target object:

SelectedTower.SendMessage("ApplyDamage", 10);

SendMessage accepts only one argument, which is passed to the function called. This argument may be anything, including a reference to a class - that’s a smart way to pass more than one argument to the function, or even read values from the script (just assign the script variable value to a class variable). Let’s suppose that you want to apply damage, inform who applied it and also read the resulting enemy health (caller script):

// declare the class DamageInfo:
class DamageInfo{
  var damage: float;
  var whoShot: Transform;
  var curHealth: float;

function DamageTarget(target: Transform){
  var dmgInfo: DamageInfo = DamageInfo(); // create a class instance
  dmgInfo.damage = 10; // define the damage to apply
  dmgInfo.whoShot = transform; // inform "I've shot you!"
  target.SendMessage("ApplyDamage", dmgInfo); // call ApplyDamage
  Debug.Log("enemy health = "+dmgInfo.curHealth); // read the current enemy health

In the turret script:

var health: float = 100;
var whoShotMe: Transform;

function ApplyDamage(dmg: DamageInfo){
  whoShotMe = dmg.whoShot; // which bastard shot me?
  health -= dmg.damage; // apply the damage
  dmg.curHealth = health; // pass the resulting health back to the caller
  if (health <= 0){
    // turret has died!
    Debug.Log("Argh! "" killed me!");

NOTE: The type of the argument passed in SendMessage must match the one declared in the target function. In this case, SendMessage passes a reference to a DamageInfo instance, and the target function (ApplyDamage) also expects a DamageInfo reference.