I’ve got a game object with the following TreeRemovalScript attached, so that the object is destroyed if the var remove changed from false to true.
private var remove : boolean = false;
function Start(){
if (remove == true){
Destroy (gameObject);
remove = false;
}
}
I’ve got another game object with a script attached, and I want to change the first script from the second script so that I can destroy the first game object.
The second script reads
function OnControllerColliderHit (hit : ControllerColliderHit){
if (hit.gameObject.tag == "hit01"){
GetComponent (TreeRemovalScript.remove) = false;
}
It’s bringing up an error of
I’ve been reading up on changing variables on one script from another, and have tried various options, but obviously haven’t got to grips with it. I’ve also bneen looking at all the code examples I could find, but couldn’t find an example like this I could use (or more likely, not one I could follow and understand)
Should I have a var in the second script?
Could someone please help by pointing me in the right direction.
Thanks.
function OnControllerColliderHit (hit : ControllerColliderHit){
if (hit.gameObject.tag == "hit01"){
hit.gameObject.GetComponent (TreeRemovalScript).remove = false;
}
But it would be easier to write this :
function OnControllerColliderHit (hit : ControllerColliderHit){
if (hit.gameObject.tag == "hit01"){
Destroy (hit.gameObject);
}
a few things
one, if you want to access remove from outside the script it cant be private
two, GetComponent is going to look for w/e you give it on the gameObject that it is on - so you cant just say getComponent to get the other object
you need to access the 1st object in the 2nd script by passing it with a
var object1 : GameObject;
and then call destroy
The object I want to destroy isn’t the object I’m hitting. It’s a different object Instantiated from a prefab.
The prefab is a tree that has the first script on it so that I can destroy the Instantiated clone.
Thanks waltermana.
I need to change the “private var” to just “var”.
Not sure I fully understand second point.
On my second script, I create
var object1 : GameObject;
but then how do I call the destroy it in the first script?
in the inspecter you can now drag the 1st object onto the script - then instead of dealing with boolean, when you get hit, you just call Destroy(object1);
That’s the problem (I think)
I have to attach the code to the prefab.
The object actually being destroyed is Instantiated from the prefab during the actual game, so it’s not available to have the script attached before the game starts.
So if I drag the prefab onto the script, will it just destroy the clone?
Thanks for the above suggestions. They don’t appear to work as I’d be trying to destroy the prefab not the clone.
What I’m trying to do is to destroy the clone by code.
I can get the clone to destruct. And I can get it to destruct after so many seconds, by altering the code directly attached to the prefab.
What I want to do is something similar, but to activate the code attached to the prefab from a code attached to another object that isn’t attached to it and which doesn’t collide with it.
When my FPC collides with something, I need the code attached to the FPC to alter the “remove” var to to “true” on the code that is attached to the prefab (the prefab var is now public, not private as it was originally). Hopefully, so that the clone is destroyed
Hope this makes better sense now, and hope someone can help.
Thanks.