• 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 Supremfire · Oct 24, 2010 at 06:09 AM · gameobjectdestroy

Problem with destroy gameObject

Hi, I have this script:

var Lumber : int = 100;

function Update(){

if (Lumber == 0){ Destroy(gameObject); } }

So when the lumber is = 0 it will destroy all the same game object from my scene. But i want to destroy only one object, how do I do this ? Can you help me? Cheers!

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 duck ♦♦ · Oct 24, 2010 at 09:58 AM 0
Share

Which game object do you want to destroy, and how are you identifying it? You need to provide more information surrounding your specific scenario to get helpful answers.

2 Replies

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

Answer by Loius · Oct 24, 2010 at 07:25 AM

Sounds like you've attached that to every Lumber object. You need local checks, not world checks, if you want to delete one at a time. I'd guess wherever you're decrementing Lumber is where you want to be deleting only the Lumber object that was just triggered.

Without any substantial details, though, that's the best you'll get - a guess.

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 Supremfire · Oct 24, 2010 at 03:04 PM 0
Share

I have a forest (made from more trees) and wen the wood is 0 to delete the tree wich have 0 wood

avatar image Loius · Oct 24, 2010 at 05:02 PM 0
Share

I hope that somewhere you have aTreeObject.lumber -= someAmount. Right after that you need to check to see if aTreeObject.lumber

avatar image Supremfire · Oct 24, 2010 at 05:56 PM 0
Share

Yes I have all this but I have all this but I have a single script on all trees so if i say Destroy(gameObject) all my trees are deleted but i want only one tree to be deleted.

avatar image
0

Answer by Anton Petrov · Oct 24, 2010 at 06:13 AM

Where is gameObject defined and how do you use it? May be this will help:

if ( Lumber == 0 && gameObject != null )
{
    Destroy( gameObject );
    gameObject = null;
}
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 Loius · Oct 24, 2010 at 07:23 AM 0
Share

gameObject ~is~ the GameObject the script is attached to.

avatar image Mike 3 · Oct 24, 2010 at 07:45 AM 0
Share

gameObject is also a property with no set function, so that wouldn't even compile

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

No one has followed this question yet.

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How to Play 3D Sound and Keep Script on Multiple Prefabs after Destroying a gameObject? 0 Answers

References to GameObject become null 1 Answer

How to Destroy gameObjects and deal damage within a radius except player ? 2 Answers

Change target after destroying it. 2 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