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 DiegoPower · Jun 19, 2014 at 02:46 PM · destroy objectitem

Destroying a wall after collecting X amount of items?

I am trying to have a wall automatically be destroyed after I collect 5 items that I have put on the level, but I am having trouble doing so. I am not the best at coding so any help would be grateful.

The following is the code I have to collect the items. Do I need to add something to this or make a new code entirely?

 //Set up variables for text output on screen, and audio
 //This will create "open slots" in your editor, on the object that has this script
 //You will drop in your GuiText and Audio into the open slots.
 public GUIText TextOutput;
 public AudioClip SoundToPlay;

 //Set up variable for number of items collected.
 //Each time you collect something, this can be updated to be you "inventory" of that item.
 public static int itemCounter = 0;    

 //Code for when the player collides with an item to collect.
 void OnTriggerEnter (Collider other)
 {
     //When something hits the collectable, if it is the player that touches the collectable,
     //Play a noise,
     //Display some text,
     //Update the inventory,
     //Delete the prefab.
     if (other.tag == "Player") {
         //Play the audio at the collectables position
         AudioSource.PlayClipAtPoint (SoundToPlay, this.transform.position);

         //Update the GuiText
         TextOutput.text = "You Collected An Item!";

         //Update the varible for the # of items you have in your inventory
         //The below code is a "shorthand" method of writing itemCounter = itemCounter +1;
         itemCounter += 1;
     
         //Simulate collection by destroying the prefab object
         Destroy (gameObject.transform.parent.gameObject);
     }

 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JayOhhh · Jun 19, 2014 at 02:58 PM

It doesn't look like you have coded the destroy method for the wall. You already have 90% of it completed as in you have the variable for the item collection and you have that method working.

All you need to do is create an if statement to check the item counter. If itemcounter satisfies your destroy wall conditions use GameObject.Find(), to get your wall and then destroy it. Keep in mind there are MULTIPLE ways to do this. This is just the first that popped to mind, and is fairly simple.

 void OnTriggerEnter (Collider other)
 {
 
 if (other.tag == "Player") {
 AudioSource.PlayClipAtPoint (SoundToPlay, this.transform.position);
  
 
 TextOutput.text = "You Collected An Item!";
 
 itemCounter = itemCounter +1;
 itemCounter += 1;
  
 Destroy (gameObject.transform.parent.gameObject);
 
 // Insert destroy method here
  if (itemCounter = 5) {
   DestroyWall();
  }
 
 }
  
 }
 
 void DestroyWall() {
 
 GameObject wallToDestroy = GameObject.Find("MyWallToDestroy");
 Destroy (wallToDestroy);
 
 }
Comment
Add comment · Show 2 · 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 DiegoPower · Jun 19, 2014 at 04:08 PM 0
Share

Still doesn't seem to be working. Do I need to have the wall tag with certain info or something else?

avatar image JayOhhh · Jun 20, 2014 at 02:22 PM 0
Share

Try putting in a debug statement in the DestroyWall() method for starters, make sure that it is getting called. The string value in the method needs to exactly match the wall that you want to destroy.

 void DestroyWall() {
  Debug.Log("Destroy wall called");
 GameObject wallToDestroy = GameObject.Find("$$anonymous$$yWallToDestroy");
  Debug.Log(wallToDestroy.name);
 Destroy (wallToDestroy);
  Debug.Log("Destroy method finished");
 }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help Scripts 0 Answers

Fixing a single call of an action (across 2 scripts) 1 Answer

i get errors but i have no idea why 0 Answers

how to make a customizable item? 1 Answer

Custom Editor Window to Create and Store Item Data 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