Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rottweiler · May 29, 2013 at 08:06 PM · randomtexure

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rottweiler · May 30, 2013 at 06:45 AM 0
Share

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?

avatar image robertbu · May 30, 2013 at 06:51 AM 0
Share

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?

avatar image rottweiler · May 30, 2013 at 07:52 AM 0
Share

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.

avatar image robertbu · May 30, 2013 at 09:05 AM 0
Share

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?

avatar image rottweiler · May 30, 2013 at 09:16 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Planetary perlin noise? My oceans are too patchy. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges