Search Unity

Validate on scene only

Discussion in 'Prefabs' started by Nixaan, Apr 6, 2020.

  1. Nixaan

    Nixaan

    Joined:
    May 30, 2013
    Posts:
    118
    After a lot of searching I'm unable to find a way to validate a value only at scene (i.e. not at prefab stage). What I mean is something like:

    Code (CSharp):
    1. public class NonPrefabValidator : MonoBehaviour
    2. {
    3.     [SerializeField] GameObject mandatorySceneTarget = default;
    4.  
    5.     private void OnValidate()
    6.     {
    7.         if (isNotPrefab && mandatorySceneTarget == null)
    8.         {
    9.             Debug.LogWarning("Target at scene/gameObject/component/coords is not inspector assigned.", this);
    10.         }
    11.     }
    12. }

    I have tried:

    Code (CSharp):
    1. #if UNITY_EDITOR
    2.             //if (PrefabStageUtility.GetCurrentPrefabStage() == null) {
    3.             if (PrefabStageUtility.GetPrefabStage(this.gameObject) == null)
    4.             {
    5. #endif
    6.                 Debug.LogWarning("Target at scene/gameObject/component/coords is not inspector assigned.", this);
    7. #if UNITY_EDITOR
    8.             }
    9. #endif
    But that still gets warnings on play. I assume that is when the initial prototype is created because it is not detected as a prefab but mandatorySceneTarget is still null.
     
    jdrandir likes this.
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    This should be correct:
    Code (CSharp):
    1. PrefabStageUtility.GetPrefabStage(this.gameObject) == null
    I'm not sure what you mean when you say that you still gets warnings on play. What warnings? Your own warning here? Could you post the entirety of the method?