• 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 Jerry.Richards · Jul 27, 2014 at 05:52 AM · gameobjecttexturearray

How do I texture objects in an array with different textures?

Hi, I am currently learning how to use unity and I am learning how to make a puzzle piece game. I have stored all the pieces in an array and have labeled them. But now I don't know how to actually texture each piece individually. Here is my code so far:

 using UnityEngine;
 using System.Collections;
 
 public class renderTexture : MonoBehaviour 
 {
     private GameObject[] blocks = new GameObject[15];
 
     void Start()
     {
         Texture image = (Texture2D) Resources.Load("Cuzz", typeof(Texture));
 
         blocks[0] = GameObject.FindGameObjectWithTag("Cube01");
         blocks[1] = GameObject.FindGameObjectWithTag("Cube02");
         blocks[2] = GameObject.FindGameObjectWithTag("Cube03");
         blocks[3] = GameObject.FindGameObjectWithTag("Cube04");
         blocks[4] = GameObject.FindGameObjectWithTag("Cube05");
         blocks[5] = GameObject.FindGameObjectWithTag("Cube06");
         blocks[6] = GameObject.FindGameObjectWithTag("Cube07");
         blocks[7] = GameObject.FindGameObjectWithTag("Cube08");
         blocks[8] = GameObject.FindGameObjectWithTag("Cube09");
         blocks[9] = GameObject.FindGameObjectWithTag("Cube10");
         blocks[10] = GameObject.FindGameObjectWithTag("Cube11");
         blocks[11] = GameObject.FindGameObjectWithTag("Cube12");
         blocks[12] = GameObject.FindGameObjectWithTag("Cube13");
         blocks[13] = GameObject.FindGameObjectWithTag("Cube14");
         blocks[14] = GameObject.FindGameObjectWithTag("Cube15");
 
 
         blocks[0].renderer.material.mainTexture = image;
     }
 
     void Update()
     {
     }
 }

Cuzz is the name of the image I am trying to put on.

Comment
Add comment · Show 3
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 jimmycrazyskills · Jul 27, 2014 at 05:55 AM 0
Share

So does blocks[0].renderer.material.mainTexture = image; set the texture for the "Cube01" object?

avatar image zharik86 · Jul 27, 2014 at 06:30 AM 0
Share

$$anonymous$$aybe change you line 10 on:

  Texture2D image = (Texture2D) Resources.Load("Cuzz", typeof(Texture));
avatar image Jerry.Richards · Aug 03, 2014 at 08:30 PM 0
Share

No worries. I figured it out. See, the block that was assigned to block[0] didn't have the proper tag. I'm telling the program to find the tag "Cube01" but there wasn't one. The block that I wanted labeled as "Cube01" was ins$$anonymous$$d tagged with "Cube". Which is why I kept getting an error. All the other blocks were labeled correctly. Only block[0] wasn't.

1 Reply

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

Answer by zharik86 · Jul 27, 2014 at 06:29 AM

Most likely for all your cubes you use one material Diffuse based on a standard shader. Therefore when you change a texture in block[0], you change a texture of a material and according to all remaining blocks. The simplest in this situation, it to make the material on each block. But you remember that it will increase draw calls (dynamic batching won't work). There is the second way, to make one material for all blocks and when you change in it a texture, create one more material, being based on the main.

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

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

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

Related Questions

Resources.Load Problem 1 Answer

arrays / textures help 1 Answer

How to check if a specific gameobject is in the array? 1 Answer

Carving the ground similar to the game "where's my water?" 0 Answers

selected object in array lost in translation 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