Search Unity

Copy texture from 1 object to another

Discussion in 'Scripting' started by Disati, Nov 12, 2011.

  1. Disati

    Disati

    Joined:
    Oct 5, 2009
    Posts:
    111
    Hi guys!

    I have this newbie scripting problem I'd like to get helped with.

    Here's the situation: I have an object ('A'') with a texture, i want this texture to be assigned on another object ('B') on mouse over (when pointing the cursor at object A).

    What I have now is the following. It only changes the texture through pre-assigned texture variables :
    Code (csharp):
    1. var oldTexture : Texture;
    2. var newTexture : Texture;
    3. private var mouseOver : boolean = false;
    4.  
    5. function Update()
    6. {
    7. if (mouseOver)
    8. {
    9. renderer.material.mainTexture = newTexture;
    10.     }
    11. }
    12.  
    13. //need to add the Assign Texture function here but I don't know how
    14. function OnMouseEnter ()
    15. {
    16. mouseOver = true;
    17.     }
    18.  
    19.  
    20. function OnMouseExit ()
    21. {
    22. mouseOver = false;
    23. }
    Methors that could be used:
    -Copy the texture of objects with a specific tag;
    -copy texture from only child objects;
    -...

    Which one I prefer is the one with the tag. But please feel free to give your own idea about this.

    Sketch:
    at Start


    when mouse over A1 the main objects 'copies' the texture on A1. Same for objects A2, A3, ...



    Note that if this problem gets solved I won't just post a short "Solved" comment here, but I'll keep this thread active for questions of other people experiencing the same problems. So by helping me out on this one you could help a dozens more.

    Thanks in advance.
     
    Last edited: Nov 14, 2011
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    OK, first what defines your "other" object? you are using predefined textures, but what you want to do is refer to another object and pull it's texture..

    renderer.material.mainTexture = otherObject.render.material.mainTexture;
     
  3. Disati

    Disati

    Joined:
    Oct 5, 2009
    Posts:
    111
    Thanks for responding, BigMisterB, I really appreciate it.

    Actually i have multiple 'A' objects, so i think this method won't be the best one when copying the texture of multiple objects to one. I've added a small sketch in the first post, maybe that'll explain what I'm looking for. Thanks though!