Saket Kumar
Saket Kumar

Reputation: 1197

Create material from code and assign it to an object

I am very new to Unity3d. I have a prefab which contains 6 quads making it a cube. I want to add image textures to different faces of cube. I am getting images from a web service, so I have to add or change material in the script. The problem I am facing is, I am not able to find material property in gameObject.

I have tried below code:

using UnityEngine;
using System.Collections;

public class shelfRuntime : MonoBehaviour {
    public GameObject bottle;
    public GameObject newBottle;
    // Use this for initialization
    void Start () {
   
            iterateChildren(newBottle.transform.root);
        
        GameObject rocketClone = (GameObject)Instantiate(bottle, bottle.transform.position, bottle.transform.rotation);
        rocketClone.transform.localScale += new Vector3(1, 1, 1);
        GameObject newBottleInMaking = (GameObject)Instantiate(newBottle, newBottle.transform.position, newBottle.transform.rotation);

        Transform[] allChildren = newBottleInMaking.GetComponentsInChildren<Transform>();
        foreach (Transform child in allChildren)
        {
            // do whatever with child transform here
        }
    }
    void iterateChildren(Transform trans)
    {
        Debug.Log(trans.name);
        if (trans.name == "Back") {
            var ting = trans.gameObject.GetComponent<Renderer>();
        //   trans.renderer.material  // there is no material property here 
        }

        // Do whatever logic you want on child objects here
        if (trans.childCount == 0) return;

        foreach (Transform tran in trans)
        {
            iterateChildren(tran);
        }
    }

    // Update is called once per frame
    void Update () {
    
    }
}

How to set material to quads? There are 6 quads inside my prefab.

Upvotes: 3

Views: 19899

Answers (1)

Programmer
Programmer

Reputation: 125455

You can no longer access some components directly in Unity. You must use GetComponent to get the component(Renderer) then access the material from it.

trans.renderer.material = ....

should be changed to

trans.GetComponent<Renderer>().material = yourNewMaterial;

Finally, when Cube or Quad is created in Unity, MeshRenderer is automtatically attached to them not Renderer. So, you might get run-time error with GetComponent<Renderer>(). Use MeshRenderer instead.

trans.GetComponent<MeshRenderer>().material = yourNewMaterial;

To create Material during run-time:

Material myNewMaterial = new Material(Shader.Find("Standard"));

The example below will create a Material, assign standard shader to it then change the texture to the texture from the myTexture variable before applying it to a GameObject.

public Texture myTexture;
void Start()
{
    //Find the Standard Shader
    Material myNewMaterial = new Material(Shader.Find("Standard"));
    //Set Texture on the material
    myNewMaterial.SetTexture("_MainTex", myTexture);
    //Apply to GameObject
    trans.GetComponent<MeshRenderer>().material = myNewMaterial;
}

Upvotes: 11

Related Questions