- Home /
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?
Answer by robertbu · May 29, 2013 at 08:18 PM
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.
Thanks for answer. Ok, so i rewrite my code. Now it looks like this:
#pragma strict
var textures : Texture[];
var rnd : int;
function Start () {
rnd = Random.Range(0,3);
renderer.material.mainTexture = textures[rnd];
Terrorist_Counter_Level_3.Hostage_tex = rnd;
}
So i make global varible that holds my rnd value. And when i pressed on object, it destroyed and on his place instantiate new ragdoll object whith next script:
#pragma strict
var textures : Texture[];
function Start () {
renderer.material.mainTexture = textures[Terrorist_Counter_Level_3.Hostage_tex];
}
And its works fine. But i assume this only works right when i have one object in the scene. Can i achieve the same results whith multiply object on the scene?
Yes you can do it with multiple objects but I cannot tell guide you without understand how you've structured your code. Where is the old object destroyed? Where is the new game object created?
Here is the part of code for destroying and instantiating object:
function On$$anonymous$$ouseDown(){
if(!Health.b_noHealth){
if(Input.Get$$anonymous$$ouseButtonDown(0)){
Destroy(gameObject);
Instantiate(score_obj,Vector3(transform.position.x,transform.position.y+2.5,transform.position.z),cam.transform.rotation);
Score.score -= 50;
Instantiate(prefab,transform.position,transform.rotation);
$$anonymous$$enu.int_health -= 1;
Health.kills_w += 1;
}
}
}
Thanks in advice.
Is the code in the original question (the code that contains rnd = Random.Range(0,3)) in the same script as the code you just posted? If not in the same script, is it on the same game object?
The code i've just posted in the parent object and the code in original post in other script attached to child object. But i can attach this two codes to one gameObject. I think its will be better.
Your answer

Follow this Question
Related Questions
How to create random movement in 2D 2 Answers
SOLVED! - Pick Number != These Other Numbers 2 Answers
Wandering enemies 1 Answer
random object creation 1 Answer