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