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

Change texture Image from Blender Importation

Discussion in 'Scripting' started by filipetakanap, Feb 22, 2022.

  1. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    Hello,

    I've made an object in blender and used an image (UV i think) to give it the color and texture.
    Unity recognizes the correct image name to use as the texture.

    I need to make several objects, all with different colors and textures, seems doll to create several different objects just to get a different color.

    I'm pretty noob in unity, can you give me some advise of what the best course of action would be?

    It has to be made by code that thriggers when I click it.

    I've tried:
    Code (CSharp):
    1. m_Renderer = GetComponent<Renderer>();
    2.                     var texture = Resources.Load<Texture2D>("Prefabs/austral background2");
    3.  
    4.                     m_Renderer.material.mainTexture = texture;
    but it gives an error saying "there is no 'Renderer' attached to ...
    EDIT: I added a "Mesh Renderer" but it doesn't change the color
    Thanks!
     
    Last edited: Feb 22, 2022
  2. ATate

    ATate

    Joined:
    Sep 13, 2018
    Posts:
    6
    I believe what you are trying to do should work.

    I assume you have MeshRenderer and MeshFilter components on your Gameobject and the Meshfilter contains your mesh imported from Blender.

    Try:
    m_Renderer.material.SetTexture("_MainTex", texture);


    Personally, if I only have one or two different colors and textures, I just add a second material and swap that instead, but your way is fine.

    Note: I believe current advice is not to use the Resources folder as you can get unused assets compiled into your project.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    While you can import textures and materials from Blender into Unity, I don't find that it adds any value.

    I use Blender3D to make geometry and unwrap it properly, using the texture only to view my work in Blender.

    Once it comes into Unity I always make custom material tweaks anyway, so I create a prefab out of the model, often adding more detail and functionality at that point, and also make a fresh material and assign it to the model at that time.

    Here's some more random Blender -> Unity info:

    Unity imports Blender3D objects as FBX via a little Python script:

    https://forum.unity.com/threads/from-blender-to-unity-problem.1073381/#post-6925811

    The Python script that Unity uses (substitute your Unity version number or search) to import:

    ./Hub/Editor/2020.2.1f1/Unity.app/Contents/Tools/Unity-BlenderToFBX.py


    More on fixing it:

    https://forum.unity.com/threads/all-my-mesh-folder-content-is-gone.1102144/#post-7094962

    Blender3D objects used as trees / detail in Unity3D terrain (See the second half of this response)

    https://forum.unity.com/threads/ter...trees-made-with-blender.1112119/#post-7155631

    https://forum.unity.com/threads/all...nging-parent-in-blender.1159283/#post-7442123
     
  4. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    I got it, had to link all the parts of the object in blender to have a single material, then add mesh renderer on the prefab.
    Thanks, i guess i'll work this way.