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 TSI25 · Oct 10, 2013 at 11:07 PM · texturerandomly

Randomly select and apply a texture?

What we're attempting to do is to randomly select a texture from a folder of textures in the asset list, and apply that texture to a game object. No matter what we try the texture doesn't seem to get applied, even if we forgo randomly generating the number and specify a specific texture it still doesn't apply. I feel like if we can figure out how to apply a texture programmatically we can get the rest to work but even after consulting the unity script reference we're still pretty stuck. here is the code we are working with so far.

 using UnityEngine;
 using System.Collections;
 
 public class TestTexture : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         GameObject.Find("Planet_3").renderer.material.SetTexture ("_MainTex",
             (Texture)Resources.Load ("Assets/Standard Assets/Terrain Assets/Planet Textures/0"));
     }
     
     // Update is called once per frame
     void Update () {
         GameObject.Find("Planet_3").renderer.material.SetTexture ("_MainTex",
             (Texture)Resources.Load ("Assets/Standard Assets/Terrain Assets/Planet Textures/0"));
     }
 }


its supposed to work by generating a random number (all of the textures are named for numbers) and then to use that number to select the desired texture and apply it to the game object.

can anyone tell us what we're doing wrong, and recommend how to fix it?

Comment
Add comment · Show 2
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 DaveA · Oct 10, 2013 at 11:25 PM 0
Share

Have you seen this? http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html

Do you really want to load a texture every frame?

Are there any errors or warnings in the console?

avatar image TSI25 · Oct 10, 2013 at 11:37 PM 0
Share

yes, when i said that we had "[consulted] the unity script reference" that is the exact page to which i was referring.

no, that was us debugging. we figured maybe the start just wasn't applying for whatever reason and so we threw it into the update to see if it would make a difference. it did not.

we have the warning "The Referenced Script on this Behavior is missing!" several times.

1 Reply

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

Answer by aldonaletto · Oct 11, 2013 at 04:08 AM

@DaveA is right: the page linked says that the resources must be in Resources folders - you specify the resource name, not its path, and Resources.Load searches in all Resources folders. You're not getting error messages because SetTexture doesn't throw any exception when the texture is null - it just does nothing. Move the textures to a Resources folder and just pass the asset name ("0", in this case).

The rest of the code seems ok - I tested the simple script below and the textures were randomly changed every second:

 var textures: Texture2D[]; // assign some textures here in the Inspector
 
 function Start (){
     while (true){
         renderer.material.SetTexture ("_MainTex", textures[Random.Range(0, textures.Length)]);
         yield WaitForSeconds(1.0);
     }
 }

NOTE: The error message you've got says that a script attached to some object was deleted, what has nothing to do with the texture problem.

Comment
Add comment · Show 1 · 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 Gregusko · Aug 14, 2014 at 09:51 AM 0
Share

Thanks Aldonaletto - this works well.
I call the random material to apply to a random ball generator I wrote and my balls now have different textures.

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

18 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Randomly floating GUI Texture 1 Answer

Assigning UV Map to model at runtime 0 Answers

How can I make a good-looking path? 0 Answers

Animated Rotten Tomato 0 Answers

Displaying final score using image textures 1 Answer


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