Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Adam 2 · Jul 14, 2010 at 11:50 PM · javascriptinstantiateprefab

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image equalsequals · Jul 15, 2010 at 02:37 AM 0
Share

In your third script, where does itemGameObject1 come from?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Adam 2 · Jul 16, 2010 at 07:08 AM 0
Share

yes i did, but for some reason when pulling this out of my testing project and inserting it into the game it worked...

avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Instantiate prefab within parameters?(Javascript) 1 Answer

Instantiate prefabs just before it comes into view 2 Answers

How to instantiate multiple prefabs in multiple positions? 0 Answers

How to instantiate a Prefab ? 2 Answers

Instantiate prefabs before it comes into view 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges