Search Unity

Palm tree material lost after executing this script.

Discussion in 'Shaders' started by keeperkai2, Dec 27, 2012.

  1. keeperkai2

    keeperkai2

    Joined:
    Oct 26, 2012
    Posts:
    14
    I'm writing a component that can change the materials of the trees/grass/detail meshes in terrain during runtime, but I've encountered some strange results. The purpose of this code was to check if/when the unity terrain component will change the shader of the material of treeprototypes when you are far away( and the terrain engine renders the tree as a billboard)
    I planned to do this by simply printing the shader name of the treeprototype every 1 second, but it only prints once(the treeprototype[0] is the palm tree included in the unity terrain package) and it seems that whenever I try to reference the shader in Update(), the material for the bark of the palm tree is lost(since the bark is the main material), and this is very very strange for me...can anyone tell me why?
    (in other words, after I push play and this component executes, the bark material of the palm tree prefab is lost forever, when I only try to print its name and did nothing to overwrite it)
    public class TestTerrain : MonoBehaviour {
    public Material mat;
    public Terrain t;
    private float age=0.0f;
    private int it=0;
    private Shader lastshader=null;
    private Shader thisshader=null;
    private Terrain terrain;
    private TreePrototype[] trees;
    // Use this for initialization
    void Start () {
    terrain=GetComponent<Terrain>();
    trees=terrain.terrainData.treePrototypes;

    Material[] tmp=new Material[trees.Length];

    }

    // Update is called once per frame
    void Update () {
    age+=Time.deltaTime;
    if(age>1.0f){
    print (it++);
    thisshader=trees[0].prefab.renderer.material.shader;
    print(thisshader.name);

    age=0.0f;
    }
    }
    }
     
  2. keeperkai2

    keeperkai2

    Joined:
    Oct 26, 2012
    Posts:
    14
    Just in case someone runs into the same problem as I did, the treeprototype GameObjects actually reference the prefabs of the trees, so if you change any material/component/...etc of those prefabs, it will be permanent, which means even when you stop the game, the changes will still be applied(and won't be rolled back).