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 Hatdatsat · Jul 02, 2013 at 03:24 PM · texturearraychangenewbie

Change texture of gameobject in array.

I finally got my texture to work but now i want to be able to change it to a other texture. I know how to do it on a single object, but now i want to try and change it while the object is in a array.

 var cubeZero : GameObject;
 var cubes : GameObject[,] = new GameObject[9,9];
 var RedColorTex:Texture2D;
 var BlueColorTex:Texture2D;
 
 function Start () {
     cubeZero = Resources.Load("Cube");
     for(x=0; x<9; x++){
         for(y=0; y<9; y++){
             var r : GameObject = Instantiate(cubeZero, transform.position, Quaternion.identity);
             r.renderer.material.mainTexture= BlueColorTex;
             cubes[x,y] = (r.gameObject);
    
             r.transform.position = Vector3(x+0.5,y+0.5,0.5);
             
             cubes[5,5].renderer.material.mainTexture= RedColorTex;
         }
     }
 }


This line gives a error. cubes[5,5].renderer.material.mainTexture= RedColorTex; But i was wondering if it is possible. I know this array type ain't a easy one to use because you can't alter much in it.

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

1 Reply

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

Answer by robertbu · Jul 02, 2013 at 03:32 PM

You should use #pragma strict on your scripts. You are missing your 'var' in your 'for()' loops. But this is not the issue that is causing you the problem. The issue is that you are trying to set Cubes[5,5] in the inner loop of your nested 'for()' loop. That is, entry [5,5] has has not been created and filled by your 'for()' loop when you try to access that entry. Move setting the texture to after the 'for()' loops:

 #pragma strict
 
 var cubeZero : GameObject;
 var cubes : GameObject[,] = new GameObject[9,9];
 var RedColorTex:Texture2D;
 var BlueColorTex:Texture2D;
 
 function Start () {
     cubeZero = Resources.Load("Cube");
     for(var x=0; x<9; x++){
        for(var y=0; y<9; y++){
        Debug.Log(x+","+y);
          var r : GameObject = Instantiate(cubeZero, transform.position, Quaternion.identity);
          r.renderer.material.mainTexture= BlueColorTex;
             cubes[x,y] = (r.gameObject);
 
          r.transform.position = Vector3(x+0.5,y+0.5,0.5);
        }
     }
     cubes[5,5].renderer.material.mainTexture= RedColorTex;
 }

Or if you are trying to set the colors as you go, use 'x' and 'y':

 if (x / 2 * 2 == x)
   cubes[x,y].renderer.material.mainTexture = RedColorText;

This will set all the cubes in which 'x' is even to the 'RedcolorText'.

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 Hatdatsat · Jul 02, 2013 at 03:40 PM 0
Share

Ooh how stupid of me, your totally right. (before this problem i was trying to make texture work and i accidentally put the line in the loop) Thanks for the help

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

15 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

Related Questions

changing the tags in an array 1 Answer

How do I keep a texture different on copied objects? 1 Answer

Array like level system to change rules. 1 Answer

Best option to change texture/material.. 0 Answers

Spell-casting combo system 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