Search Unity

Changing a png image assigned to a plane using script

Discussion in 'Scripting' started by mraeclo, Dec 10, 2016.

  1. mraeclo

    mraeclo

    Joined:
    Dec 3, 2016
    Posts:
    10
    Hello, how is it going?

    I know there is a few posts here that look like this one but none of the solutions given are working here, as you will see in the code.
    Basically, I have an asset which is a plane with a png file assigned and a Nature/Tree Soft Occlusion Leaves shader assigned (so that it can be seen by front or back). What I wanna do is instantiate many (15 here for example, but the plan is to have more than 200, so public materials and drag and drop from editor would not be the best solution) of the Frame asset, but each with a different image.
    First of all, when I assigned the png file, I drag and dropped the png file from the assets to the plane, and it created a materials folder where the images where. This is part of my doubt, technically, is it assigning a texture or a material? To the mesh or to the shader?
    Sorry for such noob questions but It makes sense because I found all this solutions to some people with similar problems but trying to change the texture or the material of the renderer does not work, although gives different results.
    So, I have the 15 png files in one folder, and the related .mat files on another. Here`s the code, lots of commented lines for trying different solutions, will comment and post screenshots of them:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class Controle : MonoBehaviour {
    7.  
    8.     public Material[] materials_flavianeSeq01 = new Material[15];
    9.     Texture[] textures_flavianeSeq01 = new Texture[15];
    10.     GameObject[] frames = new GameObject[15];
    11.     public GameObject frame;
    12.     float c;
    13.     MeshRenderer r;
    14.     public Texture a;
    15.     public Texture b;
    16.  
    17.     void Awake (){
    18.         c = 0;
    19.         for (int i = 0; i < materials_flavianeSeq01.Length; i++) {
    20.             string endereco = "flavianeSeq01/Materials/" + i.ToString() + ".mat";
    21.             //materials_flavianeSeq01 [i] = Resources.Load ("flavianeSeq01/Materials/" + i.ToString() + ".mat", typeof(Material)) as Material;
    22.             //textures_flavianeSeq01 [i] = Resources.Load ("flavianeSeq01/" + i.ToString () + ".png", typeof(Texture)) as Texture;
    23.             frames [i] = Instantiate (frame);
    24.             //frames [i].gameObject.GetComponent<MeshRenderer> ().material = materials_flavianeSeq01 [i];
    25.             //frames [i].gameObject.GetComponent<MeshRenderer> ().material = Resources.Load(endereco, typeof(Material)) as Material;
    26.             //frames [i].gameObject.GetComponent<MeshRenderer> ().sharedMaterial = Resources.Load(endereco, typeof(Material)) as Material;
    27.             //frames [i].gameObject.GetComponent<MeshRenderer> ().material.SetTexture("_MainTex",textures_flavianeSeq01 [i]);
    28.             //frames [i].gameObject.GetComponent<MeshRenderer> ().material.mainTexture =  textures_flavianeSeq01 [i];
    29.             frames [i].transform.Translate (c, 0, 0);
    30.             c -= 0.1f;
    31.         }
    32.  
    33.     }
    34.  
    35.     // Use this for initialization
    36.     void Start () {
    37.      
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update () {
    42.      
    43.     }
    44.      
    45. }
    46.  
    This first image is the result of the code as posted, the instances work fine, they change position but stay with the same image.
    Screen Shot 2016-12-10 at 5.37.05 AM.png

    Now, if I uncomment lines related to using .material or .sharedMaterial (lines 25, 26) the result is that it do instantiate the desired game objects, but without the renderer, my guess is that it does not find the materials but I`m pretty sure the path is correct, as you can see in the next image
    Screen Shot 2016-12-10 at 5.43.34 AM.png

    Next, I have tried to change texture, not material, so, If I uncomment lines 22 and 27, or 22 and 28, I get similar results, it instantiates the gameObjects with the renderer and shader but seem not to find any file, the result is that another plane appears, but only 1, which is pretty strange considering all the game objects instantiated have different positions, and they all have the renderer, screenshots:
    Screen Shot 2016-12-10 at 5.49.33 AM.png
    Screen Shot 2016-12-10 at 5.49.45 AM.png

    Well, thats it.. I hope someone has gone through this or can help somehow.

    Thanks in advance.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You don't include a file extension with Resources.Load(), so your materials probably aren't loading at all. You should always verify the results of a call like that:

    Code (csharp):
    1.  
    2. if(loadedMaterial == null)
    3. {
    4.     Debug.Log("Material failed to load.");
    5. }
    6.  
     
  3. mraeclo

    mraeclo

    Joined:
    Dec 3, 2016
    Posts:
    10
    Worked! Thanks a lot.