Hey, I’m so far progressing really well with Unity, the only obstacle I really find in it is the coding. Everything else seems to work well, with a few tutorials online coming in handy, of course. But I’ve come across a problem that I haven’t seen anyone else ask, and I need some feedback as to how I can approach it.
Before you ask, no, I suck at coding, and you’re going to have to babysit me through it.
But that’s the thing, I don’t know if coding has anything to do with this. Is there a way a sphere, which is being shined upon by a nearby light, can change it’s texture, but only in a certain area, where it isn’t being lit? Or is that not possible?
Thanks, in advance, for responding.
EDIT: I need to give you guys a mental picture of what I’m trying to achieve. I have a sphere, with a planet’s texture, and a moon rotating around it. A light, or the sun, is shining towards it from a long distance. When the sun is not exposing the texture, and the planet is in shadow, can you change it’s texture so you can see the lights of cities and such?
Are you sure you want to change materials when a light shines on an object? Why? A material defines the visual appearance of an object, including how it reflects light falling on its surface. It sounds more like you just want to define a custom shader that produces whatever visual effect you want in response to incoming light falling on the surface.
No problem if you suck at coding - try drawing a picture of what you’re trying to achieve instead…
[EDIT] your question changed while I was writing the following… Please see below for my new answer…]
A GameObject can’t have a material.
a GameObject has a renderer which has a material.
so you would do something like…
renderer.material = myOtherMaterial;
to set the material…
The easiest way to do what you describe would be to set up zones and use the OnCollisionEnter and OnCollisionExit methods to change the spheres material.
You can use primatives such as cubes or spheres to setup zones, just remove the renderer attached to them and tick the ‘is Trigger’ box in the colider.
Hope this helps.
The links below point to the docs.
OnCollisionExit
OnCollisionEnter
Material
Do the same as above with the zones but instead of changing the material do this…
renderer.material.mainTexture = newTexture;
probably easier just to change the material to one which has the new texture assigned…