• 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
Question by SterlingStudios · Aug 05, 2014 at 03:29 PM · instantiateprefabslooting

Looting Help

Major problem here. I am making a looting system. I may have to re-write it if this doesn't help. I am totally stumped on this one. I got the player to kill the enemy and instantiate the pouch, however, I don't know how to get it to destroy!!!!!!! I've tried GameObject.Find();, but that doesn't work because I have multiple objects. I also don't want to make individual prefabs, and also GameObject.Find(); cant find inactive objects. How dumb is that?!

Here is the collection piece:

 #pragma strict
 
 var minExp : float;
 var maxExp : float;
 var minGold : float;
 var maxGold : float;
 var hud : GameObject;
 private var gold : int;
 private var exp : int;
 var expGui : GameObject;
 var goldGui : GameObject;
 
 function Start () {
 
     exp = Random.Range(minExp, maxExp);
     gold = Random.Range(minGold, maxGold);
 
     hud = GameObject.Find("Loot Pouch");
     expGui = GameObject.Find("exp");
     goldGui = GameObject.Find("gold");
 }
 
 function OnTriggerStay (col : Collider) {
 
     if(col.gameObject.tag == "Player" && Input.GetKeyDown(KeyCode.E)) {
         hud.active = true;
         expGui.guiText.text = "EXP: " + exp;
         goldGui.guiText.text = "GOLD: " + gold;
     }
 }

Here is the take:

 #pragma strict
 
 var lootHud : GameObject;
 
 function Start () {
 
     lootHud = GameObject.Find("Loot Pouch Hud");
 }
 
 function OnMouseEnter () {
 
     guiText.color = Color.red;
 }
 
 function OnMouseExit () {
 
     guiText.color = Color.blue;
 }
 
 function OnMouseDown () {
 
     lootHud.active = false;
 }


Comment

People who like this

0 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 SolutionStudios · Aug 05, 2014 at 03:57 PM 0
Share

You could try disabling the scripts on the object rather than setting the object to inactive. Then GameObject.Find may work. Or you could try using tags GameObject.FindGameObjectsWithTag

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by SterlingStudios · Aug 05, 2014 at 04:16 PM

Okay, so I got the loot HUD to show up by using guiText.enabled and guiTexture.enabled, but how do I delete a specific prefab? Because I want when you click 'take', the loot pouch destroys itself, however the pouch is instantiated so how can I tell which one to destroy?

Comment

People who like this

0 Show 6 · 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 SolutionStudios · Aug 05, 2014 at 04:23 PM 0
Share

if you use GameObject.FindGameObjectsWithTag it should give you an array of all of the instances. You could iterate through this array to find the closest object to the player, which should be the instance of the pouch you are taking from.

avatar image SterlingStudios · Aug 05, 2014 at 05:09 PM 0
Share

Hmmm... How would this work though? Would you mind giving me an example?

avatar image SolutionStudios · Aug 05, 2014 at 05:53 PM 0
Share

From the documentation:

     // Print the name of the closest enemy
     print(FindClosestEnemy().name); 
     
     // Find the name of the closest enemy
     function FindClosestEnemy () : GameObject {
         // Find all game objects with tag Enemy
         var gos : GameObject[];
         gos = GameObject.FindGameObjectsWithTag("Enemy"); 
         var closest : GameObject; 
         var distance = Mathf.Infinity; 
         var position = transform.position; 
         // Iterate through them and find the closest one
         for (var go : GameObject in gos)  { 
             var diff = (go.transform.position - position);
             var curDistance = diff.sqrMagnitude; 
             if (curDistance < distance) { 
                 closest = go; 
                 distance = curDistance; 
             } 
         } 
         return closest;    
     }
 
  

just add Destroy(closest.gameObject); Apply a tag to the pouch prefab, and change the tag on the code above (8th line).

avatar image SterlingStudios · Aug 05, 2014 at 06:23 PM 0
Share

Didn't work. Here is the current code:

 #pragma strict
 
 var exp : GameObject;
 var lootBack : GameObject;
 var gold : GameObject;
 var destroy : boolean;
 
 function Start () {
 
     lootBack = GameObject.Find("LootPouchHolder");
     exp = GameObject.Find("exp");
     gold = GameObject.Find("gold");
 }
 
 function OnMouseEnter () {
 
     guiText.color = Color.red;
 }
 
 function OnMouseExit () {
 
     guiText.color = Color.blue;
 }
 
 function OnMouseDown () {
 
     lootBack.guiTexture.enabled = false;
     exp.guiText.enabled = false;
     gold.guiText.enabled = false;
     guiText.enabled = false;
 }
 
 function FindClosestEnemy () : GameObject {
         //Find all game objects with tag Enemy
         var gos : GameObject[];
         gos = GameObject.FindGameObjectsWithTag("Pouch"); 
         var closest : GameObject; 
         var distance = Mathf.Infinity; 
         var position = transform.position; 
         // Iterate through them and find the closest one
         for (var go : GameObject in gos)  { 
             var diff = (go.transform.position - position);
             var curDistance = diff.sqrMagnitude; 
             if (curDistance < distance) { 
                 closest = go; 
                 Destroy(closest.gameObject);
                 distance = curDistance; 
             } 
         } 
         
         return closest;    
     
     }

avatar image SolutionStudios · Aug 05, 2014 at 07:02 PM 0
Share

add the following code at the end of OnMouseDown:

 FindClosestEnemy();

and add the following code replacing line 39:

 var position = GameObject.Find("Player").transform.position;

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Instantiate Reference Problem 1 Answer

Prefabs wont instantiate using a for loop? 1 Answer

I need help with rotation 2 Answers

Trying to instantiate prefabs to a parent object with C# script. 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