3

Unity: 5.1.1f

Language: c#

When i instantiate a gameObject inside a Assets/Editor/ file, it doesn't appear in Scene until i select another scene's gameobject.

I've tried calling some methods like:

SceneView.RepaintAll();
HandleUtility.Repaint();

But non of them look to be working. This is how i spawn the object:

public class PrefabEditor: Editor {
    void OnSceneGUI() {
        GameObject prefabInstance = Instantiate(prefab) as GameObject;

        // assign him an icon label
        Texture2D tex = EditorGUIUtility.IconContent("sv_label_0").image as Texture2D;
        Type editorGUIUtilityType = typeof(EditorGUIUtility);
        BindingFlags bindingFlags = BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic;
        object[] args = new object[] {
            prefabInstance, tex
        };
        editorGUIUtilityType.InvokeMember("SetIconForObject", bindingFlags, null, null, args);

        EditorUtility.SetDirty(prefabInstance);
    }
}
FrakyDale
  • 647
  • 8
  • 22

1 Answers1

2

I believe you are using Editor class then instead of instantiate your gameObject like

GameObject prefabInstance = Instantiate(prefab) as GameObject;

instantiate your gameObject like this

GameObject prefabInstance = (GameObject) PrefabUtility.InstantiatePrefab(prefab);

and try to use

SceneView.lastActiveSceneView.Repaint();

I hope this helps.

Neeraj Kumar
  • 957
  • 1
  • 9
  • 20