Search Unity

I have a problem with the light

Discussion in 'Getting Started' started by piofar03, Mar 15, 2023.

  1. piofar03

    piofar03

    Joined:
    Mar 15, 2023
    Posts:
    3
    Hi. I have a problem. I have made a light in Unity and made it follow the character on screen, but when the character takes damage and dies, an error pops up that says like this:

    "MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object."

    I know what the error is, and how I could fix it, but since I'm so dumb, I don't know how to code what I want to do? Can you help me?:(:( this is the script for the light:
    Code (CSharp):
    1.    public GameObject NinjaFrog;
    2.     void Update()
    3.     {
    4.         Vector3 position = transform.position;
    5.         position.x = NinjaFrog.transform.position.x;
    6.         position.y = NinjaFrog.transform.position.y;
    7.         transform.position = position;
    8.     }
    (NinjaFrog is the object for the follow)
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    Code (CSharp):
    1.    public GameObject NinjaFrog;
    2.     void Update()
    3.     {
    4.     if(ninjaFrog == null)return;
    5.         Vector3 position = transform.position;
    6.         position.x = NinjaFrog.transform.position.x;
    7.         position.y = NinjaFrog.transform.position.y;
    8.         transform.position = position;
    9.     }
     
    piofar03 likes this.
  3. piofar03

    piofar03

    Joined:
    Mar 15, 2023
    Posts:
    3
    Thanks so much for your help!
     
    Homicide likes this.