Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
-1
Question by PaulCupi · Dec 13, 2012 at 11:28 PM · transformprefabvariabledelete

How to delete a Variable in a script in game

I have 3 blocks. I have a script where when I look at a block I want it to delete the other 2 blocks. However these two blocks are variables in my script so when they are deleted in game the script is still trying to look for them and obviously they are not there any more so the game crashes. How can I make it so as well as deleting the 2 other blocks it removes the variables from the script. I have this script on all three blocks so which ever one I look at the other two are deleted. Here's my script:

 var Girl : GameObject;
 var spawnPoint : Transform;
 var Block : GameObject;
 var Block2 : GameObject;
 
 function OnBecameVisible(){
      var clone;
     clone = Instantiate(Girl, spawnPoint.position, spawnPoint.rotation);
    Destroy(Block);
    Destroy(Block2);
 }


For each Block the variables Block and Block 2 are the other two blocks, any help would be great! Thanks.

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 Fattie · Dec 14, 2012 at 08:23 AM 0
Share

a simple thought that may advance your cause

DO NOT have scripts "on the blocks". just have the blocks hanging around. have one master script somewhere (make an empty object called "$$anonymous$$Ission Control Central")

have that "master control" script keep track of things, decide what to delete, etc.

hope it helps

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by bompi88 · Dec 14, 2012 at 12:03 AM

You could check if your gameobject is destroyed or not, and if it's not you: destroy it. You can't actually delete a variable in the script, but you can check if it's "empty" or not. If you want to "track" objects, you could use an array or list and when you destroy an object you also remove that gameobject from the list. Then you somehow have a list of active and running gameobjects in the scene.

 var Girl : GameObject;
 var spawnPoint : Transform;
 var Block : GameObject;
 var Block2 : GameObject;
 
 function OnBecameVisible(){
     var clone;
     clone = Instantiate(Girl, spawnPoint.position, spawnPoint.rotation);
 
     if(Block != null) // check if Block isn't destroyed
         Destroy(Block);
 
     if(Block2 != null) // check if Block2 isn't destroyed
         Destroy(Block2);
 }
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 PaulCupi · Dec 14, 2012 at 09:28 PM 0
Share

I don't think you understood the question, however I have fixed it now by changing the Destoy to 'block.renderer.enabled = false' meaning it will as good as deleted as nothing will happen when I see it anyway. Thanks for the help though.

avatar image bompi88 · Dec 15, 2012 at 12:40 AM 0
Share

you're right, I didn't quite understand what you were after, and took a wild guess. You should post your solution and accept it, so that others can benefit from this.

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

11 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

Related Questions

Setting Position of Spawned Prefab 2 Answers

Unity setting SerializedProperty.prefabOverride incorrectly 2 Answers

[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers

How can I set a Transform Variable in a Prefab? 1 Answer

How can I modify a variable on an instance of a prefab after I created 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