Hi,
I’m attempting to use the following to destroy a script on another object:
Destroy(transform.Find("Buggy").GetComponent(CarGo));
But it’s not working? Am I on the right track? If not how am I supposed to do it?
Thank you from,
Stuart
Hi,
I’m attempting to use the following to destroy a script on another object:
Destroy(transform.Find("Buggy").GetComponent(CarGo));
But it’s not working? Am I on the right track? If not how am I supposed to do it?
Thank you from,
Stuart
Well, before you act on an object, or a script, you have to create variables for them (don’t know why, but that’s the deal) like this:
var myBuggy = GameObject.Find("Buggy");
var myBuggyScript : CarGo= myBuggy.GetComponent(CarGo);
and then say
Destroy(myBuggyScript);
LOL! We ALL want to destroy our Buggy Scripts!
BUT! As far as I know, you can’t destroy a script. It’s there, even if it’s not in the scene. If it’s in the project, then it’s active. You can destroy the object that it’s attached to, and you can build something into the script to render it inactive, and you can render the object inactive, but you can’t destroy the script.
If that doesn’t help, then explain what you’re trying to do and why you want to destroy the script.
Hi,
Before I answer that, are you sure you want to remove just the script, or are you trying to destroy the object?
-Chilton
Hi,
You’re doing it pretty much right. Destroy will work for both objects and components.
Destroy (GetComponent (Buggy));
But as a general rule, any time you’re shortening a script like that, you’re asking for trouble.
Destroy(transform.Find(“Buggy”).GetComponent(CarGo));
I’d split this up into three steps, then check to see if they’re all actual things. It’s the only way to be sure. And in the future, unless you’re absolutely positive your script is flawless, don’t give in to this temptation
var objectIShallSlay : GameObject = transform.Find(“Buggy”);
var soonToBeDeadComponent : Cargo = objectIShallSlay.GetComponent(CarGo);
Destroy(soonToBeDeadComponent);
This will be much easier to debug.
Of course, once you’ve got it working, there’s nothing wrong with making it one line.
-Chilton
Didn’t you just, before me, a second ago, redundantly reiterate, say the same thing, repeat, what I said, had spoken, just now, before you, the same thing?
Okay, fine, but you did it like I did again… but with more style than me…
Nah, you beat me to the punch plain and simple. That’s the limit of my knowledge for tonight anyway
-Chilton
That works perfectly well actually. However, it’s true that splitting into steps makes it easier to debug.
–Eric
Hi,
I’m sorry, I didn’t read this right the first time.
The problem you’re probably having is that you’re using transform.Find. You said you’re trying to destroy a script on another object, right? That transform.Find will search the current object.
So somehow, you need to obtain a reference to the other object (raycast, GameObject public var, collision, magic genie, etc.), and do your transform.Find() on that. Does that make sense?
-Chilton
Thanks for all the help guys.
Vimalakirti’s first solution worked great.
Thanks,
Stuart.