Prefabs in git

Hi, guys.

My team experienced really unpleasant problem: we are storing some ui prefabs in our project, but every time someone saves scene he gets a bunch of prefabs, marked as changed in git. The problem is that no one is changing prefabs.

Has any one experienced this and found a solution?

Thanks in advance

This problem can be caused by a script modifying data in OnValidate(). If the data is modified unconditionally, it seems like the prefab will be marked as dirty in memory despite no changes actually being made. It will then be written to disk when the scene is next saved, triggering your source control plugin to mark the file for edit.

I’ve reproduced this effect using a prefab with a script containing this code:

#if UNITY_EDITOR
void OnValidate()
{
	transform.rotation = Quaternion.identity;
}
#endif

The problem can be fixed by changing the code to this:

#if UNITY_EDITOR
void OnValidate()
{
	if (transform.rotation != Quaternion.identity)
		transform.rotation = Quaternion.identity;
}
#endif

I was also initially confused by the whitespace differences I saw in source control. I’m using Perforce on Windows with the settings set to sync from the server with native line endings (CRLF), but Unity always seems to always write text-based assets with Unix-style line endings (LF). That means that files last touched by source control may not have the same whitespace as files last touched by Unity.