- Home /
Instantiating object from inventory
I almost have a function inventory for my project, but when i try deploying my object through my inventory, it says "the prefab you want to instantiate is null" even though the object its looking for in my assets is indeed a prefab with the same name and is blue. heres the code:
function OnGUI() {
//var openSubMenu = false;
inventoryStyle.normal.background = guiBackground;
if(iWasPressed && timesPressed == 1)
{
Time.timeScale = 0;
inventoryOpen = true;
GUI.Box(Rect(guiX, guiY, guiWidth, guiHeight), "inventory", inventoryStyle);
if(GUI.Button(Rect(15,30,75,75), slot1) && slot1Empty == false)
{
openSubMenu = true;
}
if(GUI.Button(Rect(160,30,75,75), slot2) && slot2Empty == false)
{
openSubMenu = true;
}
if(openSubMenu == true)
{
GUI.Box(Rect((Screen.width - 150)/2, (Screen.height - 100)/2, 150, 100), "drop object", subMenuStyle);
if(GUI.Button(Rect((Screen.width - 125)/2, (Screen.height - 25)/2, 50,50), "Yes"))
{
Debug.Log(deployObject.name + "is the instancing object");
var droppedObject = Instantiate(Resources.Load(deployObject.name), Vector3(transform.position.x, transform.position.y, transform.position.z + 5),
Quaternion.identity);
openSubMenu = false;
}
if(GUI.Button(Rect((Screen.width + 25)/2, (Screen.height - 25)/2, 50,50), "No"))
{
openSubMenu = false;
}
}
}
if(timesPressed < 1 | timesPressed > 1)
{
inventoryOpen = false;
Time.timeScale = 1;
}
}
function ObjectImageSwap() {
if(selectedObject.name == "testObject1")
{
slot1Empty = false;
slot1 = testImage;
if(slot1 == testImage)
{
Debug.Log("slot1 should be test image");
deployObject = gameObject.Find("testObject1");
}
}
if(selectedObject.name == "testObject2")
{
slot2Empty = false;
slot2 = testImage2;
if(slot2 == testImage2)
{
Debug.Log("slot2 should be test image2");
deployObject = gameObject.Find("testObject2");
}
}
}
if(!slot1)
{
slot1 = defaultImage;
slot1Empty = true;
}
if(slot1)
{
slot1Empty = false;
}
if(slot1 == defaultImage)
{
slot1Empty = true;
}
if(!slot2)
{
slot2 = defaultImage;
slot2Empty = true;
}
if(slot2 == defaultImage)
{
slot2Empty = true;
}
if(slot2 == testImage2)
{
deployObject = gameObject.Find("testObject2");
}
its basically telling me that there is an object to instantiate. however when i call the instantiate line of code, it returns saying the prefab is null.
basically, my code says when a ray hits an object, return the name, assign that name with an image for the inventory. if i click that button with the image of an object, i want it to instantiate it by finding a prefab with the name of that object.
it displayed the object name which was whatever deployobject is, and then it said "is the instancing object". so it would say "testObject1 is the instancing object" is that what you're asking?
well this is basically a test run, and im trying to make the script as dynamic and self sufficent as i can. so, even though there is already the object in the scene, im just trying to instantiate it again in the inventory, since i have to click on that object anyways for it to be in my inventory. im trying your suggestion now
it didnt work unfortunately. its the same as i had it before, except deployObject is now a string from the start. its hard to explain... it looks for the gameObject through gameObject.find in the beginning when the object is in the scene, and then IDEALLY i want to destroy after i figure out how to instantiate it
Your answer
Follow this Question
Related Questions
New GUI and Inventory problem. 1 Answer
Inventory Instantiation problem 1 Answer
Error : Missing GUILayout.EndScrollView/EndVertical/EndHorizontal? UnityEditor.DockArea:OnGUI() 1 Answer
How to skip certain objects in List, in a for loop? GUI related. 1 Answer
Setting the parent of a transform which resides in a prefab error 1 Answer