Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change object texture

Discussion in 'Scripting' started by KarneeKarnay, Feb 3, 2013.

  1. KarneeKarnay

    KarneeKarnay

    Joined:
    Sep 3, 2012
    Posts:
    61
    I'm currently trying to edit some code to see if an object in an array has a higher x value than 4 in distance between itself and its previous member. From my code I know that it happens but the texture doesn't change. Any help?



    Code (csharp):
    1.  
    2. public Texture tester;
    3.  
    4. for(int i = 0; i < arrayLength.Length; i++)
    5.         {              
    6.             if(arrayLength.Length-1 != null)
    7.             {              
    8.                 transform.position = new Vector3(spawnPosX + random_x * LengthRadius, spawnPosY + random_y * HeightRadius+1.2f, spawnPosZ  + random_z * LengthRadius);
    9.             }
    10.            
    11.             platSpwans = arrayLength[i].transform.position;
    12.                
    13.             arrayLength[i].transform.position = new Vector3(platSpwans.x + random_x * LengthRadius, platSpwans.y + random_y * HeightRadius+1.25f, platSpwans.z  + random_z * LengthRadius);
    14.            
    15.             if (i > 1)
    16.             {
    17.            
    18.                 if (arrayLength[i].transform.position.x > (arrayLength[i -1].transform.position.x+4))
    19.                 {
    20.                     arrayLength[i].renderer.material.SetTexture("test", tester );
    21.                     Debug.Log("true");
    22.                 }
    23.             }
     
  2. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,249
    I assume arraylength is gameobjects?

    I would use .GetComponent(Renderer) and place that in the variable like rend

    Then go rend.material = the new material and make a different material for each of the changes you want.