0
\$\begingroup\$

How can I spawn or draw a sphere with a custom material with a script?

So far I've tried using Gizmos.DrawSphere, but I can't find a way to control the material used to draw it.

I would like to draw it with a material with some specularity, around 0.5.

\$\endgroup\$
4
  • \$\begingroup\$ A gizmo with specularity is a somewhat odd situation. Usually Gizmos are just helper/debug shapes drawn in the editor with a simple non-specular shader for speed. Can you explain more about your situation, what this sphere represents, and whether you need it in the scene view, the game view, or both, in editor or at runtime? \$\endgroup\$
    – DMGregory
    Sep 7, 2018 at 11:27
  • \$\begingroup\$ Not gizmo, material would be for sphere which is created by Draw Sphere function. \$\endgroup\$
    – eon
    Sep 7, 2018 at 11:34
  • \$\begingroup\$ If you're calling DrawSphere via the Gizmo class, then the resulting sphere is a spherical gizmo. If you don't want a gizmo, can you edit your question to explain more about what you need from this sphere so we can understand and help you find a more fitting way to draw it? (For instance, why not spawn a sphere primitive into your scene, with a MeshRenderer you can assign a custom material to? Once we know why you don't want to go that route, we can suggest good alternatives for your needs) \$\endgroup\$
    – DMGregory
    Sep 7, 2018 at 11:38
  • \$\begingroup\$ I didn't know about it. I would like to spawn spheres via script and create materials for them in script. \$\endgroup\$
    – eon
    Sep 7, 2018 at 11:43

1 Answer 1

1
\$\begingroup\$

You can spawn a new spherical GameObject and set its material like so:

var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);

var renderer = sphere.GetComponent<Renderer>();

renderer.sharedMaterial = myMaterial;

Or if you just want the sphere to be drawn, without having a GameObject for it in your scene, you can use Graphics.DrawMesh / Graphics.DrawMeshNow / Graphics.DrawMeshInstanced, passing a spherical mesh and the material you want to use (or setting the material with Material.SetPass before using the "now" version)

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .