- Home /
Get update calls from prefabs when they are changed.
[Update: See comment, renamed question for more accuracy]
Hello everybody
I have a confusing problem, probably a bug.
I've a script, that manages level of details. There is one script that manages the different lod-objects. When I deactivate this script, I want all lod levels to be visible. This script should also work in edit mode and has the [ExecuteInEditMode] attached.
I set the meshrenderer with the aid of a subroutine:
private void SetMeshRenderer (GameObject obj, bool newState)
{
if (obj.renderer != null)
{
obj.renderer.enabled = newState;
}
for (var i = 0; i < obj.transform.childCount; i++)
{
SetMeshRenderer(obj.transform.GetChild(i).gameObject, newState);
}
}
When I call this subroutine from the method "OnEnable()", everything works fine, the meshrenderers of the lod levels are updated correctly. But when I use this very same subroutine in the method "OnDisable()", then I get sometimes a bug.
When the script was deactivated from an object in the 3D scene (GameObject), everything works fine. But when the script was deactivated from the prefab in the library, then the meshrenderers are getting activated (checked with debuging...), but on the prefab and on the instances in the scene, nothing changes...
It's somehow difficult to describe, because I do not actually understand the problem myself...
Here's the code I use for the two methods:
void OnEnable ()
{
// Works fine here
foreach (var level in LodLevels)
{
if (level.LodObject== null)
{
continue;
}
SetMeshRenderer(level.LodObject, true);
}
}
void OnDisable()
{
foreach (var level in LodLevels)
{
if (level.LodObject== null)
{
continue;
}
SetMeshRenderer(level.LodObject, true);
}
}
Hope somebody is able to reproduce the error. If not, then I'm going to write an error report...
Greetings
Chillersanim
After some testing, I found out, that the prefab doesn't call the $$anonymous$$ethods from $$anonymous$$onoBehaviour (OnEnable, Update, etc.). Because of that, when activating/deactivating the prefab, his lod-objects don't get updated correctly and all instances just copy the object states.
So now I need to know, if there is a way to get if the prefab was changed or not (i.e. Is the changing object a prefab or a gameobject).
Has somebody an idea how to accomplish this?
Greetings
Chillersanim
Answer by whydoidoit · Feb 17, 2014 at 04:56 PM
Prefabs don't maintain a link to the instances when the game is running - so changing the prefab will not change anything. You need to change each individual instance of the prefab in the scene.
Yes I know, but because I'm working on a lod system that works also in the editor, I need to know when a prefab is updated. In the game, everything works fine, I only have problems in the editor.
Greetings
Chillersanim.
Ah right that makes sense, well I don't think you can do that. You can tell if an instance is different to the parent, but I can't remember or find any documentation on a way of getting that as a prefab changed notification.
Hmmm, I will search a bit further. But in the meantime will I solve this problem with a force update button...
Greetings
Chillersanim
When I would use a custom inspector editor, would it be possible, that this custom editor can get the desired information?
If yes, then I could use that as solution, because the problem realy only occurs in the editor...
Gretings
Chillersanim
You mean if it changes using a custom editor? Yes you could do that.
If you are only worried about your own fields then it would also be possible by using a checksum of the fields of the object done on a regular basis. UnitySerializer could compute you such a checksum.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133628im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
If there is only 1 instance of a class, should it be Static? - C# 2 Answers
Call a Void on Collision 2 Answers
AI Patrolling 1 Answer