Skip to main content
Tweeted twitter.com/StackGameDev/status/763947303933665281
added 161 characters in body
Source Link
TobiasW
  • 987
  • 6
  • 20
  • 37

I want to instantiate a prefab within the context menu (Components). I got the code to create the entry in the context menu, but I can't figure out how to place my prefab in the scene. My current code is somehow like this :

using UnityEngine;
using UnityEditor;
using System.Collections;

public static class MenuItemGameobject {

    [MenuItem("GameObject/Create RedBlue GameObject", false, 12)]
    static void Init()
    {
        Debug.Log("here");
        //new GameObject("RedBlue GameObject"); // This adds an empty GO
        //new GameObject((GameObject.Find("UIPlane"))); //This duplicates the GO if it is already in the scene
        //PrefabUtility.InstantiatePrefab(); //This is the method which I guess is needed, but I don't know how to get the GO parameter
    }
}

So again, I want to add my prefab exactly like you add a cube within the context menu.

Edit : This is how a public variable looks like in the inspector :

enter image description here

I want to instantiate a prefab within the context menu (Components). I got the code to create the entry in the context menu, but I can't figure out how to place my prefab in the scene. My current code is somehow like this :

using UnityEngine;
using UnityEditor;
using System.Collections;

public static class MenuItemGameobject {

    [MenuItem("GameObject/Create RedBlue GameObject", false, 12)]
    static void Init()
    {
        Debug.Log("here");
        //new GameObject("RedBlue GameObject"); // This adds an empty GO
        //new GameObject((GameObject.Find("UIPlane"))); //This duplicates the GO if it is already in the scene
        //PrefabUtility.InstantiatePrefab(); //This is the method which I guess is needed, but I don't know how to get the GO parameter
    }
}

So again, I want to add my prefab exactly like you add a cube within the context menu.

I want to instantiate a prefab within the context menu (Components). I got the code to create the entry in the context menu, but I can't figure out how to place my prefab in the scene. My current code is somehow like this :

using UnityEngine;
using UnityEditor;
using System.Collections;

public static class MenuItemGameobject {

    [MenuItem("GameObject/Create RedBlue GameObject", false, 12)]
    static void Init()
    {
        Debug.Log("here");
        //new GameObject("RedBlue GameObject"); // This adds an empty GO
        //new GameObject((GameObject.Find("UIPlane"))); //This duplicates the GO if it is already in the scene
        //PrefabUtility.InstantiatePrefab(); //This is the method which I guess is needed, but I don't know how to get the GO parameter
    }
}

So again, I want to add my prefab exactly like you add a cube within the context menu.

Edit : This is how a public variable looks like in the inspector :

enter image description here

added 8 characters in body
Source Link
TobiasW
  • 987
  • 6
  • 20
  • 37

I want to instantiate a prefab within the context menu (Components). I got the code to create the entry in the context menu, but I can't figure out how to place my prefab in the scene. My current code is somehow like this :

using UnityEngine;
using UnityEditor;
using System.Collections;

public static class MenuItemGameobject {

    [MenuItem("GameObject/Create RedBlue GameObject", false, 12)]
    static void Init()
    {
        Debug.Log("here");
        //new GameObject("RedBlue GameObject"); // This adds an empty GO
        //new GameObject((GameObject.Find("UIPlane"))); //This duplicates the GO if it is already in the scene
        //PrefabUtility.InstantiatePrefab(); //This is the method which I guess is needed, but I don't know how to get the GO parameter
    }
}

So again, I want to add my prefab exactly like you add a cube within the context menu.

I want to instantiate a prefab within the context menu (Components). I got the code to create the entry in the context menu, but I can't figure out how to place my prefab in the scene. My current code is somehow like this :

using UnityEngine;
using UnityEditor;
using System.Collections;

public static class MenuItemGameobject {

    [MenuItem("GameObject/Create RedBlue GameObject", false, 12)]
    static void Init()
    {
        Debug.Log("here");
        //new GameObject("RedBlue GameObject"); // This adds an empty GO
        //new GameObject((GameObject.Find("UIPlane"))); //This duplicates the GO if it is already in the scene
        //PrefabUtility.InstantiatePrefab(); //This is the method which is needed, but I don't know how to get the GO parameter
    }
}

So again, I want to add my prefab exactly like you add a cube within the context menu.

I want to instantiate a prefab within the context menu (Components). I got the code to create the entry in the context menu, but I can't figure out how to place my prefab in the scene. My current code is somehow like this :

using UnityEngine;
using UnityEditor;
using System.Collections;

public static class MenuItemGameobject {

    [MenuItem("GameObject/Create RedBlue GameObject", false, 12)]
    static void Init()
    {
        Debug.Log("here");
        //new GameObject("RedBlue GameObject"); // This adds an empty GO
        //new GameObject((GameObject.Find("UIPlane"))); //This duplicates the GO if it is already in the scene
        //PrefabUtility.InstantiatePrefab(); //This is the method which I guess is needed, but I don't know how to get the GO parameter
    }
}

So again, I want to add my prefab exactly like you add a cube within the context menu.

added 133 characters in body
Source Link
TobiasW
  • 987
  • 6
  • 20
  • 37

I want to instantiate a prefab within the context menu (Components). I got the code to create the entry in the context menu, but I can't figure out how to place my prefab in the scene. My current code is somehow like this :

using UnityEngine;
using UnityEditor;
using System.Collections;

public static class MenuItemGameobject {

    [MenuItem("GameObject/Create RedBlue GameObject", false, 12)]
    static void Init()
    {
        Debug.Log("here");
        //new GameObject("RedBlue GameObject"); // This adds an empty GO
        //new GameObject((GameObject.Find("UIPlane"))); //This duplicates the GO if it is already in the scene
        //PrefabUtility.InstantiatePrefab(); //This is the method which is needed, but I don't know how to get the GO parameter
    }
}

So again, I want to add my prefab exactly like you add a cube within the context menu.

I want to instantiate a prefab within the context menu (Components). I got the code to create the entry in the context menu, but I can't figure out how to place my prefab in the scene. My current code is somehow like this :

using UnityEngine;
using UnityEditor;
using System.Collections;

public static class MenuItemGameobject {

    [MenuItem("GameObject/Create RedBlue GameObject", false, 12)]
    static void Init()
    {
        Debug.Log("here");
        //new GameObject("RedBlue GameObject"); // This adds an empty GO
        //new GameObject((GameObject.Find("UIPlane"))); //This duplicates the GO if it is already in the scene
    }
}

So again, I want to add my prefab exactly like you add a cube within the context menu.

I want to instantiate a prefab within the context menu (Components). I got the code to create the entry in the context menu, but I can't figure out how to place my prefab in the scene. My current code is somehow like this :

using UnityEngine;
using UnityEditor;
using System.Collections;

public static class MenuItemGameobject {

    [MenuItem("GameObject/Create RedBlue GameObject", false, 12)]
    static void Init()
    {
        Debug.Log("here");
        //new GameObject("RedBlue GameObject"); // This adds an empty GO
        //new GameObject((GameObject.Find("UIPlane"))); //This duplicates the GO if it is already in the scene
        //PrefabUtility.InstantiatePrefab(); //This is the method which is needed, but I don't know how to get the GO parameter
    }
}

So again, I want to add my prefab exactly like you add a cube within the context menu.

Source Link
TobiasW
  • 987
  • 6
  • 20
  • 37
Loading