Issues with scripts losing references and being auto-updated when I switch back to a branch

Hey guys,

This is somewhat of a follow up post to this issue I reported here

When I return to certain branches in my project, some scripts lose all their references in the inspector. In this instance I have attached some updates that have appeared as soon as I checked out a branch I have not been on for a few weeks.

In a lot of cases, Github Desktop picks up on files having changed when they havent. In other cases, files have auto-updated as seen in the image. If I go in to the script and literally just add a white space, this is resolved when it compiles, but its very inconvenient for settings on scripts to be lost each time.


Hello nostalgicbear,

It sounds like you’re experiencing issues with script references in Unity when switching branches, which may be related to how Unity handles serialized data and how version control (Git) manages changes to your scripts. Here are a few steps and considerations that might help you with the problem:

  1. Check Serialized References: When you switch branches, any changes to serialized references (like your public variables in Unity) might not be recognized if the corresponding scripts have been modified significantly. Ensure that your scripts are consistent across branches.

  2. Unity Cache: Unity maintains a cache for metadata which may be leading to outdated references. You can try clearing the cache or re-importing the assets in Unity. You can do this by going to Assets -> Reimport All.

  3. Git Ignore Settings: Ensure that your .gitignore file is correctly configured to avoid ignoring files that are critical for Unity, like Library and Temp folders. These are auto-generated by Unity and should not be committed.

  4. Change Detection Configuration: Sometimes Git may incorrectly detect changes. Double-check your Git configuration to ensure it’s set to handle certain file types (like .cs or Unity-related files) properly.

  5. Script Modifications: You mentioned adding whitespace to resolve issues. If you’re editing the script files, ensure they are properly saved and committed so when switching branches, the latest reference is intact.

  6. Branch Strategy: Consider adopting a feature branching strategy where changes in scripts are well isolated. This minimizes conflicts when merging branches.

  7. Unity Version: Sometimes issues can arise due to the Unity version you are using. Ensure you’re working with a stable version across your branches.

If possible, please provide additional context about your Unity version or any specific details from the Copy Project Info Into Clipboard feature, and I would be happy to assist you further.

In the meantime, you can check the Needle Engine documentation for potential version control best practices as well.

Let me know how it goes!

1 replies left - Get a PRO license for more AI replies.
thread_q8bkmtMQxRIEwi2aBaGf0Xnk
Reply to this message to continue the conversation

Hi, is Unity focused/Open when you switch branches? Both branches have the “CricketTrailTracker” component in this example?

It sounds like during switching Unity forgets about these classes either because they dont exist in both branches OR because they’re not recompiled. I didn’t see this issue here before so it’s only a guess right now.

As a workaround: Can you try adding @dont-generate-component at the top of your typescript component to tell the Component compiler to not run for the next Needle Engine component it finds?

Hey Marcel,

Yes Unity was open all times as far as I can remember, and yes, I can confirm the scripts that have been affected dont exist in both branches. I’ll check if the same issue persists with Unity closed.

Yes I can also add the @dont-generate-component too.

With regard to GitHub Desktop picking up changes to the .cs files, when no changes are present, have you seen that before?

Thanks for your help as always.

I can confirm the scripts that have been affected dont exist in both branches

Oh so it’s correct that CricketTrailTracker doesnt exist on both branches?

I’ve seen files being marked as dirty during branch changes / afterwards when they were touched, yes (e.g. Unity with meta files sometimes)