- Home /
making prefabs at runtime?
Here i have an inventory script that i've been working on. It works fine, but when i drop an object, its dropping an instance of it (a clone), and when i go to pick it back up, i can't re-drop it since its a clone, not a prefab. Can i make a prefab of it at runtime? Here's code: function InventoryRay() {
var hit : RaycastHit;
Debug.DrawRay(transform.position, transform.forward * rayLength, Color.red);
if(Physics.Raycast(transform.position, transform.forward, hit, rayLength))
{
if(Input.GetMouseButtonDown(0))
{
if(hit.collider.gameObject.tag == "collectable")
{
Debug.Log("collectable");
currentGameObjectName = hit.collider.gameObject.name;
Debug.Log(currentGameObjectName);
menuItems.Add(currentGameObjectName);
itemToDestroy = GameObject.Find(currentGameObjectName);
Destroy(itemToDestroy);
}
}
}
}
function OnGUI() {
buttonPosX = 10;
buttonPosY = 10;
if(openInventory == true)
{
GUI.Box(Rect(0, 0, Screen.width/2, Screen.height/2),"INVENTORY");
for(items in menuItems)
{
if(GUI.Button(Rect(buttonPosX, buttonPosY, 100, 50), items))
{
openSubMenu = true;
currentItem = items;
}
buttonPosX = buttonPosX + 125;
}
if(openSubMenu == true)
{
SubMenu();
}
}
}
function SubMenu() {
var btnHeight = 50;
var btnWidth = 50;
var boxHeight = 150;
var boxwidth = 250;
GUI.Box(Rect( (Screen.width/2) - (boxwidth/2), (Screen.height/2) - (boxHeight/2), boxwidth,boxHeight), "what do you want to do?");
if(GUI.Button(Rect((Screen.width/2) - (boxwidth/2) + 25, (Screen.height/2) - (boxHeight/2) +50, btnHeight, btnWidth), "Drop"))
{
spawnPos = Vector3(player.transform.localPosition.x, player.transform.localPosition.y, player.transform.localPosition.z + 25);
objectToDrop = Instantiate(Resources.Load(currentItem), spawnPos,Quaternion.identity);
objectToDrop.name.Replace("(Clone)", currentItem);
menuItems.Remove(currentItem);
openSubMenu = false;
}
if(GUI.Button(Rect((Screen.width/2) - (boxwidth/2) + 100, (Screen.height/2) - (boxHeight/2) +50, btnHeight, btnWidth), "Use"))
{
Debug.Log("no function for this item");
}
if(GUI.Button(Rect((Screen.width/2) - (boxwidth/2) + 175, (Screen.height/2) - (boxHeight/2) +50, btnHeight, btnWidth), "Back"))
{
openSubMenu = false;
}
}
Answer by by0log1c · Mar 25, 2012 at 11:56 PM
I think you're taking the incorrect approach. From what I understand the real issue is that your naming convention is broken by the fact that instantiated GameObject have "(Clone)" added to their name, right? Then don't pollute your project with clone prefabs uselessly, just rename the instantiated objects so they match the original name.
In your Submenu() method, simply add this line after the instantiation:
objectToDrop.name = objectToDrop.name.Replace("(Clone)",""); //might be " (Clone)"
so i would replace the empty quote with lets say, "object1" in this case?
actually..when i do that, it just says the prefab i want to instantiate is null... i will update the code so you can see. It also doesnt change the name in the hierarchy, or in game.?
Never$$anonymous$$d! i just figured it out. It was pretty much what you said but i just did this ins$$anonymous$$d objectToDrop.name = currentItem; Thanks a bunch! im accepting your answer since it nearly gave me what i needed.
Answer by rutter · Mar 25, 2012 at 11:57 PM
You can use Instantiate()
to clone any object, whether it's one from your scene or something you loaded in with Resources.Load()
. It looks more like your problem has to do with trying to load a scene object from resources, which isn't necessary if the object is already in your scene.
Your answer
Follow this Question
Related Questions
Instantiate a prefab vs create one dynamically at runtime? 1 Answer
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers
Trouble working with a Custom class, JavaScript. 2 Answers
Prefabs Transforms LookAt 0 Answers
Instantiating scripts with references to prefabs loses the reference. 1 Answer