Want to use the same script

I want to use the same script for another object however there has to be something slightly different from the other how can I accomplish this without copying the whole thing and changing 1 to 2 lines and then have the script on the other object?

-Jan Julius

It would be helpful if you posted this script here and told us more specific what do you want to change.

Simply out of the blue the easiest way I can imagine would be to add a public boolean variable, which you can access through the inspector and surround this 1-2 lines with an if statemet which depends on that variable.

This way you can change the behaviour of the script with one click.

Use a global script to keep the variables in that the two script share …

 // Set an array of the soldiers health 
var soldierHealth : float[] = new float[20];

Then tag each game object Soldier1, Soldier2, Soldier3 … ect

Then extract the number from the tag in each game objects script …

function ConvertToInt(stringContainingNumber : String) : int{
   return System.Convert.ToInt32(stringContainingNumber.Substring(stringContainingNumber.ToList().FindIndex(function(c) char.IsDigit(c))));
}

then use that to get the game objects health by gaining access to the globalVars script from each game objects script …

// The GameObject with the GlobalVars.js containing all the Variables for the game
private var globalVars : GameObject;
// script name as the Type
private var globalVarsScript : GlobalVars;
private var soldierNumber : int;

function Start () {

    soldierNumber = ConvertToInt(transform.tag);
	globalVars = GameObject.Find("Global Vars");
	globalVarsScript = globalVars.GetComponent(GlobalVars);
}

function Update(){

        globalVarsScript.soldierHealth[soldierNumber] = 100;
}

function ConvertToInt(stringContainingNumber : String) : int{
   return System.Convert.ToInt32(stringContainingNumber.Substring(stringContainingNumber.ToList().FindIndex(function(c) char.IsDigit(c))));
}

I’m sure there are lots of different ways to accomplish this, but this is the way I do it.

if

You can tag your two objects, make them be classified.

Object1’s tag = Object without something
Object2’s tag = Object with something

than you can add a “if” statement like this

If(gameObject.tag == "Object with something"){
    // Do your slightly different
}

It won’t do something if the object’s tag is not “Object with something”