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

Issue with custom inspector making changes to objects it shouldn't

Discussion in 'Scripting' started by JoWi_Uniform, Nov 26, 2019.

  1. JoWi_Uniform

    JoWi_Uniform

    Joined:
    Jul 14, 2017
    Posts:
    15
    Hi there,
    I've recently gotten into creating custom inspectors for some simple editor tools, I've run into a few issues but mostly overcome them.

    I've now run into a strange issue and I'm not sure if I'm going crazy or if it's something I just don't understand about how editor scripts work.

    In the code below this is basically what I'm doing:
    • Wait for click in scene
    • On click, create new GameObject
    • Add a component to that object
      • That component has a public variable (furnRef.FurnitureName) of an AssetReference from the Addressables pipeline
    • Set that AssetReference to match the one currently set in my master object (Furnplacer.assetRef)
    • Then set position, scale, etc.


    This mostly works as expected BUT the issue is that when I change the AssetReference in my master object any objects created by it are also updated when they should stay as they were when created.

    If I make a change to the script, and the editor recompiles, any objects created beforehand will no longer be affected by the master changing.

    I've been trying to figure out what it is, I've tried a lot of different potential fixes, debugging, and read a lot of stuff online but I can't for the life of me figure out what is causing this.
    Is there some basic part of editor code that I'm missing that explains this behaviour? is it a bug?

    If anyone can help, make any suggestions, or even test the code in your version of unity if you're a saint I'd be very grateful, anything helps!

    Cheers,
    Joe.


    Code (CSharp):
    1.  private static bool m_editMode = false;
    2.         public static FurniturePlacer FurnPlacer;
    3.  
    4.         void OnSceneGUI()
    5.         {
    6.             FurnPlacer = (FurniturePlacer)target;
    7.  
    8.             if (m_editMode)
    9.             {
    10.                 Selection.activeGameObject = FurnPlacer.gameObject;
    11.  
    12.                 if (Event.current.type == EventType.MouseDown)
    13.                 {
    14.                     //Spawning object with asset reference
    15.  
    16.                     AssetReference assetRef = FurnPlacer.FurnToPlace;
    17.  
    18.                     GameObject NewObj = new GameObject();
    19.                     FurnitureReference furnRef = NewObj.AddComponent<FurnitureReference>();//Getting my component which I'll add the AssetReference to
    20.                     furnRef.FurnitureName = assetRef;//Setting the asset reference
    21.  
    22.                     //Set position, location etc.
    23.                     //...
    24.                     //...
    25.                 }
    26.             }
    27.         }
     
  2. JoWi_Uniform

    JoWi_Uniform

    Joined:
    Jul 14, 2017
    Posts:
    15
    Problem solved, thanks to @Luxgile on the Unity discord for the suggestion, it simply required changing:

    AssetReference assetRef = FurnPlacer.FurnToPlace;
    to
    AssetReference assetRef = new AssetReference(FurnPlacer.FurnToPlace.AssetGUID);
     
    Last edited: Nov 27, 2019