- Home /
Instantiate a Prefab thats been passed through 2 scripts?
ok so my issue is this. I have an inventory script and an item script (much like the one from HERE ). When you pick up an ite it sends a command to run the function to add the item to my inventory. heres the script:
public var InventoryIcon : Texture2D; var WeaponPrefab: GameObject; static var ItemName : String = "Anchor Hammer"; static var WeaponType: String = "Great Hammer"; static var SkillNeeded: String = "30 Attack"; static var defense : String = "0"; static var magic : String= "0"; static var ranger : String= "0"; static var strength : String = "16"; static var price: int = 28900;
private var doWindow : boolean = false;
function OnMouseOver() { doWindow=true; } function OnMouseExit() { doWindow=false; }
function OnGUI() {
if (doWindow) GUI.Window (0, Rect (110,10,200,140), DoWindow, "Info"); }
function OnMouseDown() { var inv = FindObjectOfType(inv2); inv.AddItem1( WeaponPrefab, InventoryIcon,ItemName,WeaponType,SkillNeeded,defense,magic,ranger,strength,price); Destroy(this.gameObject); Debug.Log("item picked"); }
function DoWindow (windowID : int) {
GUI.Label (new Rect(5, 15, 200, 25), "Item: " + ItemName);
GUI.Label (new Rect(5, 30, 200, 25), "WeaponType: " + WeaponType);
GUI.Label (new Rect(5, 60, 200, 25), "Skill needed: "+SkillNeeded);
GUI.Label (new Rect(5, 45, 90, 25), "Defense: " + defense);
GUI.Label (new Rect(5, 75, 90, 25), "Magic: " + magic);
GUI.Label (new Rect(5, 90, 90, 25), "Range: " + ranger);
GUI.Label (new Rect(5, 105, 90, 25), "Strength: " + strength);
GUI.Label (new Rect(5, 120, 90, 25), "Price: " + price);
}
now the AddItem1 function is this:
function AddItem1( worldObject : GameObject, texRep : Texture2D, ItemName : String, TypeOfItem : String, SkillNeeded:String,ItemDefense:String,ItemMagic:String,ItemRange:String,ItemStrength:String,ItemPrice:int)
{
if(!itemSlotUsed1){
itemGameObject1 = worldObject;
itemImage1 = texRep;
itemSlotUsed1 = true;
nameOfItem1 = ItemName;
typeOfItem1 = TypeOfItem;
skillForItem1 = SkillNeeded;
defenseOfItem1 = ItemDefense;
magicOfItem1 = ItemMagic;
rangeOfItem1 = ItemRange;
strengthOfItem1= ItemStrength;
priceOfObject1= ItemPrice;
}
//more script thats the same exact as this for other slots though
and when i try to instantiate it using this:
if(GUI.Button(Rect(Screen.width-500,Screen.height-640,60,60),itemImage1)){
if(SellShopOpen){
itemImage1 = emptyTex;
itemSlotUsed1 = false;
MoneyScript.MONEY += priceOfObject1*.7;
} else {
if (Event.current.button == 0)
Debug.Log ("Equip.");
else if (Event.current.button == 1) {
Instantiate(itemGameObject1,SpawnPoint.position,Quaternion.identity);
itemImage1 = emptyTex;
itemSlotUsed1 = false;
}
}
}
it says the prefabs not there, which its the one form the hiearchy, any idea why?
In your third script, where does itemGameObject1 come from?
Answer by cncguy · Jul 15, 2010 at 05:45 AM
Have you declared itemGameObject1? I know you haven't posted your whole script but it appears you may be assigning to a local variable in addItem1. If this is the case then declare itemGameObject1 at the start of the script.
yes i did, but for some reason when pulling this out of my testing project and inserting it into the game it worked...
Answer by StephanK · Oct 23, 2010 at 02:26 PM
You are assigning your worldObject to itemGameObject. Then you delete worldObject. So when you want to instantiate itemGameObject the reference to itemGameObject is null. (You deleted it)