Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Changing child objects' materials

Discussion in 'Scripting' started by BlitheD, Oct 5, 2006.

  1. BlitheD

    BlitheD

    Joined:
    Aug 28, 2006
    Posts:
    36
    I am trying to change the texture of an object's child object when the main object gets clicked on. I tried using the script below but received the warning "NullReferenceException" and "UnityEngine.Component.get_renderer ()". So I am doing something wrong, but I don't know what! Here's the script. Any ideas?

    transform.Find("child").renderer.material.mainTexture = texture;
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Should work...the child object is literally called "child"? If not then either change its name to "child" or use its actual name in the script. And texture is declared like "var texture : Texture2D" elsewhere, and has a texture dragged onto it in the inspector?

    --Eric
     
  3. BlitheD

    BlitheD

    Joined:
    Aug 28, 2006
    Posts:
    36
    You're right. It should work - it does. I was using multiple objects and confusing the ones I was testing on. However, I didn't have the "2D" on "Texture" and added it. Is it needed? Is it different from just "Texture"?
    Thanks for you help!
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Good question...I always use "Texture2D" as the type, but I just tried "Texture" and they seem to do the same thing.

    --Eric
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Texture is more general. For example you could also assign a RenderTexture to it or a CubeMap. May or may not be what you want.