Modify prefab via script at runtime?

Hi All,

Still working on my “get the feet wet with Unity” Asteroids clone. I have a laser projectile prefab that has a trail renderer component. When the projectile hits the edge of the screen, I warp it to the other side (ala Asteroids).

Unfortunately, that causes the trail renderer to go a bit crazy as it may be at the left edge and be attached to a projectile that just got warped to the right edge. For that instant, the trail draws entirely across the screen.

I attempted to fix this by locating the trail renderer at projectile warp time and setting its “enabled” property to “false”, hoping I could just turn it off.

Setting a break point just after setting the property to false shows that the renderer is still enabled, and thus still draws completely across the screen during the warp.

So, now I’m wondering if it’s not possible to change anything regarding the makeup of a prefab at runtime? I guess that might make sense since it’s a clone, but if that’s the case, what’s the best way to solve my problem?

Thanks,

Jeff

You can’t change the prefab itself, but you can change anything about the clone instances. I’d guess you’re not getting the right reference for the renderer to turn off.

–Eric

Eric,

Yeah, you’re right (as usual… ;)). Checking further, I see that I had a reference to mesh renderer component, not the trail renderer. Now that I have the proper reference, the trail disables as expected…

Thanks again.

Jeff