• 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 woofsy · Jun 15, 2014 at 02:27 AM · colliderobjectdestroy

Object Not Destroying?

I have a simple script setup to collect an object and destroy it. It's displaying it's text but not destroying the object. As well, it's giving me a Console message saying "Object reference not set to an instance of an object" and refers to line 47, which is where the line to destroy the object is. Any help??

 using UnityEngine;
 using System.Collections;
 
 public class Item : MonoBehaviour
 {
 
 
 
     ///////////////////////////////////////////////////////////////////////
     ///This file goes on a Collectable Game Object that is in your scene///
     ///////////////////////////////////////////////////////////////////////
 
 
 
     //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 · 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 Nick4 · Jun 15, 2014 at 03:42 AM 0
Share

Are you positive that the game object that this script is attached to, has a parent game object?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Intensity3000 · Jun 15, 2014 at 04:01 AM

If this script is on the object you want destroyed, then you should write: Destroy(gameObject), or if it is on the parent, I'm not sure how you would access it directly through script, but you could create a variable for it in the inspector, drag and drop it onto the variable, and reference that variable in the Destroy() function

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make an event happen after an object is collided with 2 Answers

destroy object in front of an other object? 1 Answer

Problem with destroying a gameObject when used with targeting 1 Answer

Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer

Need some help an object collider that stops a timer then displays it on another scene 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