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 TheCount · Dec 23, 2013 at 11:22 PM · javascriptgameobjectdestroystartup

Destroy GameObject Variable?

     var Zap : GameObject;
     
     function Start() {
     Destroy(Zap);
     }

My code is shown above. I place the game object Zap into that variable slot and then i get the error "The name 'Zap' does not denote a valid type ('not found')

I just want to destroy the OBJECT!!

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 drulludanni · Dec 24, 2013 at 12:27 AM 0
Share

I just tried the code and it worked fine for me perhaps you need to drag and drop the game object you want to destroy into the script?(add it to a random object then drag the object you want to destroy from wherever it is in the hierarchy tab into the inspector script)

3 Replies

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

Answer by clunk47 · Dec 24, 2013 at 01:57 AM

Why would you guys want him to declare it as a new gameObject when it's obviously a public variable? Just be sure since your variable is public, you assign the gameObject in the inspector, then check that it's not null before you destroy it.

 #pragma strict
 var Zap : GameObject;

 function Start()
 {
     if(Zap != null)
         Destroy(Zap.gameObject);
 }
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 sparkzbarca · Dec 24, 2013 at 01:59 AM 0
Share

because if he is so new he doesnt know how that works i've no idea if its an assigned public variable.

avatar image clunk47 · Dec 24, 2013 at 03:23 AM 0
Share

Well then tell him to assign it in the inspector? If you want to assign at runtime via code,

 private var Zap : GameObject = new GameObject();

 function Start()
 {
     if(Zap != null)
         Destroy(Zap.gameObject);

 }

Or just Destroy(Zap), since it is already defined as a GameObject.

avatar image
0

Answer by sparkzbarca · Dec 24, 2013 at 12:45 AM

zap must not actually be equal to anything it must be null already.

basically if you do for example

Zap : GameObject = new GameObject();

then Destroy(Zap)

or you assign Zap through the editor window to something it'll work

but basically what you need to understnad is declaring a variable doesnt make it equal to something or do anything nessacarily.

It's confusing because some objects like numbers have a built in constructor so when you do for example

int x;

x actually exists now it is in fact a number with its default value which happens to be 0 thats coded in, when an int is created it automagically has a value

but not all classes do except a few intergral classes things like gameobjects require the new keyword to be assigned memory.

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
avatar image
0

Answer by Cheetoturkey · Dec 24, 2013 at 01:10 AM

Zap : GameObject = new GameObject;

Destroy(Zap.gameObject)

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

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

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

Trying to make a simple inventory: 0 Answers

Confused about GameObject and Transform 1 Answer

Erasing a gameobject if you have the coordinates. 3 Answers

What is wrong with my 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