Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Script not working properly after restarting Unity

Discussion in 'Editor & General Support' started by RenegadeSloth, May 8, 2019.

  1. RenegadeSloth

    RenegadeSloth

    Joined:
    May 2, 2019
    Posts:
    2
    I'm having a very weird issue with a camera controller script. The script is written so that the camera is supposed to follow the player when they are within certain coordinates. When I first wrote the script, it worked with no issues. However, after closing and restarting Unity, the script no longer works. It is enabled, the player is still set in the inspector, and debug messages will display, but the camera does not follow the player. If I remove the script and re-add it, it starts working again. The script is attached. I am running Unity 2019.1.0f2. Any ideas why this is happening?
     

    Attached Files:

  2. JustAnotherDude

    JustAnotherDude

    Joined:
    Oct 28, 2013
    Posts:
    279
    You probably have another script moving the camera.
     
    Joe-Censored likes this.
  3. RenegadeSloth

    RenegadeSloth

    Joined:
    May 2, 2019
    Posts:
    2
    Hmmm I'm not sure that is the case. I only have 2 scripts in the project, and the other one is for player movement. It has no references to the camera in it. If there was another script controlling the camera, the camera controller script still wouldn't work after removing it and re-adding it. Once I re-add it, it works as intended every time I run, until I restart Unity again.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you had another script moving the camera in LateUpdate that might explain the issue. The order that LateUpdate between scripts could be altered the next time you load the project.

    For example, say you have 2 scripts moving the camera both in LateUpdate, and the script you posted here is Script 2. Script 1 moves the camera, then script 2 does. Since you are setting an absolute position in script 2 you never actually see what script 1 is doing - everything looks fine. You exit and reload the project. Now for whatever reason script 2's LateUpdate is being called before script 1. Now you never see the movement from script 2 and you think there is some kind of bug, when the bug is in having two different scripts moving the same object to different places when you don't intend to.

    The problem script could be from a 3rd party asset or some Unity component by the way.
     
  5. Free_guy

    Free_guy

    Joined:
    Feb 27, 2020
    Posts:
    7
    Did you find a solution?
    Having the same issue.
    But mine is not a follow script.
     
  6. Free_guy

    Free_guy

    Joined:
    Feb 27, 2020
    Posts:
    7
    I found the perfect solution
    Just duplicate the gameobject (one with the script)
    And delete the initial one .
     
    jhonrymat likes this.
  7. andriaabashidze

    andriaabashidze

    Joined:
    Mar 9, 2021
    Posts:
    1
    it worked thank you
     
  8. jhonrymat

    jhonrymat

    Joined:
    Sep 7, 2017
    Posts:
    1
    Tambien me funcionó gracias..!