How to get the same texture of other object?

So i have a bot, which have random texture assignment. There is script:

#pragma strict
var textures : Texture[];
private var rnd : float;

function Start () {
rnd = Random.Range(1,4);

if(rnd == 1){
	renderer.material.mainTexture = textures[0];
	}
	
if(rnd == 2){
	renderer.material.mainTexture = textures[1];
	}
	
if(rnd == 3){
	renderer.material.mainTexture = textures[2];
	}
}

And i need to destroy this object and instantiate the ragdoll object with the same texture. Is there possible? Or i need make 3 prefabs with different texture and 3 ragdoll prefabs?

Note, you can rewrite your script as:

#pragma strict
var textures : Texture[];
var rnd : int;
 
function Start () {
	rnd = Random.Range(1,4);
    renderer.material.mainTexture = textures[rnd];
}

In addition to simplifying the logic a bit, I’ve made ‘rnd’ public and turned it into an ‘int’. A float is not right here, and because of the differences between the float version and the int version of Random.Range(), you would have had an out-of-bounds error with this code. As long as this object exists, you can now read it from another script:

See:

http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

You will have to read it before you destroy the object. I don’t know how your transition to ragdoll is handled, so I cannot give you specifics, but assuming a third party script kills this object and Instantiates the ragdoll, you script on the ragdoll would be the same except for the call to Random.Range().

#pragma strict
var textures : Texture[];
var iTex : int;
 
function Start () {
    renderer.material.mainTexture = textures[iTex];
}

You would set iTex just after you instantiate the ragdoll.