Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Send Texture to a shader via script not attached to the 3D object

Discussion in 'Shaders' started by rimiki, Jun 17, 2015.

  1. rimiki

    rimiki

    Joined:
    Dec 30, 2014
    Posts:
    102
    Dear,

    I'm trying to send a texture to a shader using this line:

    GetComponent<Renderer>().material.SetTexture("_MainTex", Tex2);

    It works when I attach the script to the object which contain the material and the shader.

    In my case I have to send a texture to a shader but my script is not attached to the 3D object but in the camera.

    Is there any way to do that?

    Please I'm looking forward to your help. Thank you.
     
  2. rimiki

    rimiki

    Joined:
    Dec 30, 2014
    Posts:
    102
    Any help... !
     
  3. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Just add a "public GameObject go;" to your script. Then you will have a slot to assign the gameObject you want to change the texture on your script. Then you will be able to do something like go.GetComponent<Renderer>().material.SetTexture("_MainTex", Tex2);
     
  4. rimiki

    rimiki

    Joined:
    Dec 30, 2014
    Posts:
    102
    Thank you. I think I works !
     
  5. DogF

    DogF

    Joined:
    Dec 31, 2013
    Posts:
    29
    Good work!