I want to add a sphere at runtime via script.

I have the following code which compiles fine. At runtime however, no sphere is added to the scene.

I have attached the debugger to the script, and I have set breakpoints at each line.

After the line

Material nMat = new Material(Shader.Find("Lit"));//I'm using the URP

the debugger "blanks" out, it doesn't hit the next line.

What am I doing wrong?

Thank you!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SphereMaker : MonoBehaviour
    private GameObject _Sphere;

    void Start()
        _Sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);

        MeshRenderer nRenderer = _Sphere.AddComponent<MeshRenderer>();
        Material nMat = new Material(Shader.Find("Lit"));//I'm using the URP

        if (nMat == null)
            Debug.LogError("no mat!!\n");
        nMat.color = Color.red;
        nRenderer.material = nMat;

        _Sphere.transform.localScale = new Vector3(0.4f, 0.4f, 0.4f);
        _Sphere.transform.position = new Vector3(0, 0, 0);

  • 1
    \$\begingroup\$ From your past two questions, it looks like you need to pay closer attention to the error logs in your console. \$\endgroup\$
    – DMGregory
    Jun 6 '20 at 0:04

The shader was not found.

The right name for it would be:

Material nMat = new Material(Shader.Find("Universal Render Pipeline/Lit"));//I'm using the URP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.