- Home /
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.
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
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);
}
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.
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.