x


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;
}

}

more ▼

asked Mar 25 '12 at 11:28 PM

Kag359six gravatar image

Kag359six
102 20 31 33

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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)"
more ▼

answered Mar 25 '12 at 11:56 PM

by0log1c gravatar image

by0log1c
2.1k 6 9 18

so i would replace the empty quote with lets say, "object1" in this case?

Mar 26 '12 at 08:42 PM Kag359six

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.?

Mar 26 '12 at 08:45 PM Kag359six

Nevermind! i just figured it out. It was pretty much what you said but i just did this instead objectToDrop.name = currentItem; Thanks a bunch! im accepting your answer since it nearly gave me what i needed.

Mar 26 '12 at 08:56 PM Kag359six
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 25 '12 at 11:57 PM

rutter gravatar image

rutter
5.2k 2 11

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3333
x1677
x1259
x374
x224

asked: Mar 25 '12 at 11:28 PM

Seen: 810 times

Last Updated: Mar 26 '12 at 08:56 PM