Search Unity

Copying objects via Instantiate results in incorrect material instance

Discussion in 'General Graphics' started by lockbox, Feb 28, 2017.

  1. lockbox

    lockbox

    Joined:
    Feb 10, 2012
    Posts:
    519
    I have an application that allows a user disassemble and reassemble components - like an engine. When I manually disassemble things, the copying works fine and I don't have any material issues. When I automatically disassemble things - using the same scripts that are used for the manual process, it may or may not instance the material and when it does instance the material, it has the right name, but the material itself incorrect - so the color might turn out white or red instead of what it's supposed to be. I'm not using a texture atlas, and I don't copy the material and apply it to the copy. The original objects use the standard shader, set to opaque, just the color. Code is pretty simple:

    GameObject copy;
    copy = (GameObject)Instantiate (actualTransform.gameObject, actualTransform.position, actualTransform.rotation);

    The problem is consistent and the same parts always end up being the wrong color.

    Any ideas?

    Thanks!
    Rein D
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Are you able to create and share a small example project to show the issue?
     
  3. lockbox

    lockbox

    Joined:
    Feb 10, 2012
    Posts:
    519
    Karl, In the process of preparing a sample project for you, I discovered what was happening. It's not exactly as I described it. I missed the delay that happens between when the user is shown what part to remove and when the user actually removes the part. I've been working on this project for 9 months now, based on an engine that I wrote the year prior, so I totally missed the fact that I modify the renderer and flash the part before it's the user's turn to take an action. So what it was doing, was copying the part while it was flashing, resulting in the changed color. It was doing it consistently on the same parts, but not all parts. That's also what threw me off. I added some if statements to address the problem, so it's all good.

    Bests,
    Rein