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 uanmanarmy · Apr 07, 2015 at 03:25 PM · instantiateprefabprefabsmaterialstextures

How can I make that everyTime I change the texture of material, it won't change texture of every Instantiated Object that has that material

Hello.

I have a prefab called Cube, and I am instantiating at the mouse position (onClick). But before that I have a small panel with different types of textures. After I click on one of this texture, I access the material that the cube has, and change it's _MainTexture.

 using UnityEngine;
 using System.Collections;
 
 public class AssignMaterial : MonoBehaviour {
     
     private static Vector3 size;
     private Renderer s;
     public Texture newTexture;
     private Material material;
     public string materialName;
     //rayCast.transform.GetComponent<Renderer>().material.SetTexture("_MainTex", newTexture);
     // Use this for initialization
     void Start () 
     {
         size = transform.localScale;
         material = Resources.Load("Materials/" + materialName) as Material;
     }
     
     // Update is called once per frame
     void Update () 
     {
 
     }
 
     public void OnClickDown()
     {
         material.SetTexture ("_MainTex", newTexture);
         Debug.Log (material.name);
         transform.localScale = new Vector2(0.75f, 0.75f);
     }
 
     public void OnClickUp()
     {
         transform.localScale = size;
     }
 }
 

This script is assigned to the specific Image(texture) in the panel I have created.

Now for example I have texture A and B, so first I click on texture A so this texture A will be assigned on the material, and when I instantiate a Cube it's texture will be A. That works perfect. Now when for example if I click on texture B so now the material will have texture B (good), but the problem is that the texture of the Cube instantiated before will also have now texture B. So from Texture A->B.

How can I make so that every Cube will have it's own texture. Shell I duplicate the material every time I instantiate a cube or shell I still use the same material for all of the Cubes but with some kind of hacks.

Hope you guys will help me. Please if you have some questions please ask.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Veldars · Apr 07, 2015 at 03:31 PM

Hi,

I think you can use the Renderer component on your GameObject insteed of changing the Texture on your generic Material.

 private Renderer _renderer;
 public Texture newTexture;
 
 void Start() {
   _renderer = GetComponent<Renderer>();  
 }
 
 public void OnClickDown()
 {
     _renderer.material.mainTexture = newTexture;
 }
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 uanmanarmy · Apr 07, 2015 at 03:33 PM 0
Share

hmm, the current object is just an RawImage. But I will try to access the prefabs renderer (if it's possible).

avatar image
0

Answer by _Gkxd · Apr 07, 2015 at 03:34 PM

Instead of changing the material that you load using Resources.Load, you want to use the material that the renderer component uses. In the renderer component, there is a distinction between material and sharedMaterial.

Changing material.SetTexture to GetComponent().material.SetTexture should work.

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 uanmanarmy · Apr 07, 2015 at 03:36 PM 0
Share

so you say to access the renderer of the Cube(that is a prefab) and change the texture with GetComponent()?

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

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

Related Questions

how to call the animation of prefab 0 Answers

best practices for importing and updating models 0 Answers

Prefabs losing Material connections after re-opening scenes. Bug? 3 Answers

Unity not Instantiating Prefabs Properly 0 Answers

Preventing parts of a prefab from rotating w/parent? 0 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