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 Alayna · May 18, 2012 at 06:21 PM · texturematerialtag

How To Change Multiple GameObject's Textures

Hello, I have alot of objects that I want to change the texture of in runtime, instead of referencing each one. I first tried to use a tag to achieve this by using this script;

 var yourNewTexture : Texture2D;
 var ball = GameObject.FindGameObjectsWithTag("Ball");
 
 function OnMouseDown() { 
     ball.renderer.material.SetTexture("_MainTex", yourNewTexture);

 }

but I get this error BCE0019: 'renderer' is not a member of 'UnityEngine.GameObject[]'.

Secondly I tried to use shared material to change the material that affects the balls, using this script;

 var yourNewTexture : Texture2D;
 var similarObject : GameObject;
 
 
 function OnMouseDown() { 
     similarObject.renderer.sharedMaterial.SetTexture("_MainTex", yourNewTexture);
 }

When I used this script, all the balls change to the new color, but when I go out of play in the editor, the balls are permanently set to that color. For some reason the shared material keeps the change no matter what afterwards.

How would i either fix the first script (preferable for me) or make the second script not keep the changes after exiting the game? Thanks in advance for any help!

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

3 Replies

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

Answer by flamy · May 18, 2012 at 06:48 PM

var ball = GameObject.FindGameObjectsWithTag("Ball");

according to this line ball is a not a game object, it is an array of gameObjects, since FindGameObjectsWithTag returns an array instead of single element. So your code should be

 var yourNewTexture : Texture2D;
 var ball = GameObject.FindGameObjectsWithTag("Ball");
 
 function OnMouseDown() { 

    for(int i=0;i<ball.Length;i++)
     {
         ball[i].renderer.sharedMaterial.SetTexture("_MainTex", yourNewTexture);
     }
 
 }


C# Version:

 public Texture2D yourNewTexture;
 public GameObject[] ball;

 void Start()
 {
     ball = GameObject.FindGameObjectsWithTag("Ball");
 }
 void OnMouseDown() { 
 
    for(int i=0;i<ball.Length;i++)
     {
         ball[i].renderer.sharedMaterial.SetTexture("_MainTex", yourNewTexture);
     }
 
 }

if only one object in scene has ball tag use FindGameObjectWithTag so you will get a single game object instead of array. in this case you dont have to change you code except removing an 's'!!

Comment
Add comment · Show 4 · 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 hijinxbassist · May 18, 2012 at 06:50 PM 0
Share

touché, 33 sec b4 my post LOL

avatar image flamy · May 18, 2012 at 06:54 PM 0
Share

lol i saw this for sometime and w8ed someone to post, since it is so simple :P but none did for 2 or 3 $$anonymous$$ so i posted ;) btw ya touche

avatar image hijinxbassist · May 18, 2012 at 09:30 PM 1
Share

@flamy hahaha, i did the same exact thing. Too funny

avatar image Alayna · May 22, 2012 at 01:26 PM 0
Share

So I tried this, had to fix some things that the editor was complaining about, but pretty much the same. I get a few errors when I try though. First I get "You are not allowed to call this function when declaring a variable." then "FindGameObjectsWithTag can only be called from the main thread." In the end it gives me an array list that if I drag and drop the objects seperately it will change them but ignore if they are tagged or not, Is there something I'm missing?

Edit: Never$$anonymous$$d I got it to work with your help, thank you :)

avatar image
0

Answer by hijinxbassist · May 18, 2012 at 06:48 PM

The first method doesnt work because you are basically saying

 GameObject[].renderer....

An array does not have a renderer, each game object does however. So instead you need to call each of the objects individually.

 var yourNewTexture : Texture2D;
 var ball = GameObject.FindGameObjectsWithTag("Ball");
 
 function OnMouseDown() { 
     for(var i:int=0;i<ball.length;i++)
     {
         ball[i].renderer.mainTexture=youNewTexture;
     }
 }

I switch from shared material, because as you saw before, this changes the texture in the assets folder permanently. Instead change main texture on the renderer.

Comment
Add comment · 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
0

Answer by Brizz104 · Nov 12, 2012 at 05:07 PM

Can I please get a c# version of this? The only hangup I'm having is on var ball....don't know that to call it in c#. Thanks!

Comment
Add comment · Show 2 · 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 flamy · Nov 12, 2012 at 06:43 PM 0
Share

i have update the answer check!!

avatar image Brizz104 · Nov 16, 2012 at 02:14 AM 0
Share

ah thank you! didn't even know that GameObject[] existed!

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

7 People are following this question.

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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Select colour then change texture 0 Answers

new Color() not working properly? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to assign and find Textures by tag 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