Search Unity

Adding Lights at Runtime (URP)

Discussion in 'Scripting' started by luzcamacho, Apr 26, 2022.

  1. luzcamacho

    luzcamacho

    Joined:
    Mar 15, 2022
    Posts:
    2
    Hi everyone!

    Decided to finally post about this because this compilation error has been driving me insane, even though I think the solution should be very simple.

    I'm trying to add in a light to my scene via a script. I taking the sample code from the Unity scripting guide as such:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine.Rendering.Universal;
    6.  
    7. public class TestCreateLight : MonoBehaviour
    8. {
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         GameObject lightGameObject = new GameObject("The Light");
    13.         Light lightComp = lightGameObject.AddComponent<Light>();
    14.         lightComp.color = Color.blue;
    15.         lightGameObject.transform.position = new Vector3(0, 5, 0);
    16.  
    17.         // UniversalAdditionalLightData lightData = lightGameObject.AddComponent<UniversalAdditionalLightData>();
    18.     }
    19. }
    This sample code returns the following compilation errors:

    Error 1:
    Assets\Scripts\TestCreateLight.cs(12,43): error CS0311: The type 'Light' cannot be used as type parameter 'T' in the generic type or method 'GameObject.AddComponent<T>()'. There is no implicit reference conversion from 'Light' to 'UnityEngine.Component'.

    Error 2:
    Assets\Scripts\TestCreateLight.cs(13,27): error CS0029: Cannot implicitly convert type 'UnityEngine.Color' to 'System.Collections.Generic.List<float>'

    Any clue as to what I need to do to resolve these compilation errors? Am I missing an import? The strange part is that I can add an UniversalAdditionalLightData component just fine. It even creates a GameObject with a Light component, which I can't access because GetComponent<Light> gives a similar compilation error.
     
  2. luzcamacho

    luzcamacho

    Joined:
    Mar 15, 2022
    Posts:
    2
    Resolved!

    Update: Don't have an obscure public class called "Light" in a script tucked away in your project...