• 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 jammerjar · Jul 13, 2011 at 09:20 AM · textureinstance

Create a different texture for each instance

I am creating multiple instances of a silver foil balloon. I need each balloon to use a different image from a web cam. I thought the easy way to do this would simply to be update a jpg out side of Unity and then the instance would simply use that new image when the next one was created. But all the instances change to the new image.

How do you create a different texture for each instance?

Comment
Add comment · Show 6
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 Waz · Jul 13, 2011 at 10:34 AM 1
Share

Edited from open-mike comedy act into an actual question.

avatar image Waz · Jul 13, 2011 at 10:35 AM 0
Share

How are you currently setting the image on the instance? (If you're doing it in code, show that code)

avatar image Waz · Jul 13, 2011 at 11:49 AM 0
Share

Actually, it sounds like you're just seeing asset updates. That doesn't happen with a real game, only in the Editor.

avatar image jammerjar · Jul 15, 2011 at 04:12 AM 0
Share

Thanks for your input Warwick. Have been tied up at work and only just heading back to this project this weekend.

So my script is pretty simple im still new at this. "var Texture:Texture2D;

function Update () { if (Input.GetButtonDown("Fire1")) {

   if(Texture) 
   { 
      Debug.Log("Texture Loaded Sucessfully..."); 
      renderer.material.mainTexture = Texture; 
   } 
   
   
   else 
   { 
      Debug.Log("Unable to Load texture..."); 
   } 
  } 
 }"  

will try running out of editor.

Thanks for any input.

avatar image jammerjar · Jul 15, 2011 at 04:20 AM 0
Share

Damned your right but not how I had hoped. When I build the images of course dont update at all. So the scrip I was using is pretty much useless.

Any ideas?

Show more comments

2 Replies

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

Answer by Waz · Jul 15, 2011 at 04:30 AM

The simplest way to load images in-game is to use the WWW class - it can read files or web content - this let's you load new textures. Once youre loading new images and assigning to mainTexture, the gallons will end up with their own textures and your other problem will go away. Start reading here: http://unity3d.com/support/documentation/ScriptReference/WWW.html

Comment
Add comment · Show 3 · 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 jammerjar · Jul 15, 2011 at 04:57 AM 0
Share

Thanks for hanging in with me Warwick. I saw the www class and got scared off by it but will go back and have another look on your recommendation.

Does it have to come from the web or can I look to the local drive?

avatar image Waz · Jul 15, 2011 at 07:44 AM 0
Share

Sure, just prepend file:/// to the filename.

avatar image jammerjar · Jul 15, 2011 at 08:25 AM 0
Share

Warwick that's brilliant - haven't tried it on local files yet but working brilliantly online. Thanks heaps! I was about to give up. The only trouble is I am getting an error InvalidCastException: Cannot cast from source type to destination type. I think this is to do with how I am initiating the texture.

So if you don't $$anonymous$$d ...

So I have the script to get the new image placed on the object I am creating an instant of

" var url = "http://www.ciri.org.nz/idot/tradeairFEED/bagish.jpeg"; //"http://images.earthcam.com/ec_metros/ourcams/fridays.jpg"; function Start () {

var www : WWW = new WWW (url);

yield www;

renderer.material.mainTexture = www.texture; //

}"

But to get this to be triggered I am using this script on the same object.

"function Update () { if (Input.GetButtonDown("Fire1")) { if(Texture) { Debug.Log("Texture Loaded Sucessfully..."); renderer.material.mainTexture = Texture; } else { Debug.Log("Unable to Load texture..."); } } }"

As i said that works fine but....i get

InvalidCastException: Cannot cast from source type to destination type. Which is because of "renderer.material.mainTexture = Texture"

Obviously I am doing something really stupid so you wisdom is much appreciated.

avatar image
0

Answer by Graham-Dunnett · Jul 13, 2011 at 11:04 AM

Maybe use your large texture with all the webcam images tiled into it, then use the Material TextureOffet and TextureScale to select which parts of the texture to use. If you had say 16 webcam images in a 4x4 grid, then you'd use a TextureScale of 1/4 (0.25) and then the offsets would be 0.0, 0.25, 0.5, 0.75 in each direction. See http://unity3d.com/support/documentation/ScriptReference/Material-mainTextureScale.html for a code example.

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 jammerjar · Jul 15, 2011 at 04:15 AM 0
Share

hmmm not sure if that will do the trick. The images are going to be generated just before the instance is created so each time I'm looking for a new instance - this might happen 100s of times so the offset would become impractical?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Texture change problem 0 Answers

[solved] How to save instance of the texture? 2 Answers

Material Cloning Issues 1 Answer

Change the texture on a material at runtime without creating an instance 1 Answer

How can I set the material on an instance after creating it? 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