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 Kag359six · Mar 25, 2012 at 11:28 PM · instantiateprefabruntimeinventory

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


}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by by0log1c · Mar 25, 2012 at 11:56 PM

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)"
Comment
Add comment · Show 3 · 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 Kag359six · Mar 26, 2012 at 08:42 PM 0
Share

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

avatar image Kag359six · Mar 26, 2012 at 08:45 PM 0
Share

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

avatar image Kag359six · Mar 26, 2012 at 08:56 PM 0
Share

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

avatar image
0

Answer by rutter · Mar 25, 2012 at 11:57 PM

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate a prefab vs create one dynamically at runtime? 1 Answer

[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers

Trouble working with a Custom class, JavaScript. 2 Answers

Prefabs Transforms LookAt 0 Answers

Instantiating scripts with references to prefabs loses the reference. 1 Answer


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