Help, is this right?

Basically there’s a variable it can change from 0 to 1. By default is set to 0, but when i enter the trigger it changes to 1. If the variable is set to 1 i can hit a keyboard letter and destroy the object, if not nothing happenes.
That’s the code

var questProgress = "0";

function OnTriggerEnter(){
questProgress = "1";
    if(Input.GetKeyDown ("f")  questProgress == 1){
       Destroy(this.gameObject);
}
}

But the object doesn’t destroy. I guess i made an error :face_with_spiral_eyes:
Thanks

Well, there’s 2 things…

One:
The OnTriggerEnter function is a default function for collision detection between two objects. If you want to do this, try this:

function OnTriggerEnter(Collider : hit){
	if(hit.transform.tag == "objtag"){
		//dostuff	
	}
}

Two:

If collision between is not your case, try using another name for the function.

And btw, the right form to destroy an GameObject is to :

 GameObject.Destroy(gameObject)

Another mistake you made in the if condition was to use only one , the right form:

if(Input.GetKeyDown ("f")  questProgress == 1){

You’ve also declared questProgress with a string value of “0” and are trying to compare it to an integer value of 1 (no quotes). My guess is that you took #pragma strict out to get it to compile which is generally not a good idea.

Additionally, make sure you compare values of the same type. JS allows that dynamic type thingy, but i doubt the comparison of “1” and 1 will ever be true, at least this wouldn’t work in a C language.

You could still compare a single char value with an numeric value, but you’d need the offset in the ascii or unicode table anyways.

Furthermore, i don’t know if that was your intention: it might not work as you expect because while the trigger function is being executed you’d need to press the key down, if you’re to late it won’t do anything at all if i’m not mistaken.

Last but not least: in the current code there is no need too check whether the variable is 1 ( ‘1’ or “1”), whatever you wanted to use)… If you set it to 1 ( ‘1’, “1”) without a condition that will avoid this, it will always be set to that value and thus this part will always be true and can be left out.

Take out the quotes. Quotes denote a string, while you want to compare integers. You should look at data types.

@Mukabr: Why do you say that GameObject.Destroy() is better than Object.Destroy()? I see several problems with GameObject.Destroy(), for one it’s not documented. It is unclear as to what GameObject.Destroy() even means, most likely it is calling Object.Destroy() anyways. I personally don’t even like calling just Destroy() because you really don’t know where in the inheritance hierarchy it is being caught (is it Component.Destroy(), MonoBehaviour.Destroy(), etc…).

Thank everybody. BTW i copied the wrong code, now it’s fixed.
Thanks

Hey Daniel, GameObject.Destroy() it’s the same thing as Object.Destroy, since GameObject inherit Object. Since you pass as a parameter the thing you want to destroy, it’s pretty easy to see what are removing like :

Object.Destroy(gameObject.GetComponent<gameObject.GetComponent<MyScript>()); this will remove the script MyScript from gameObject, and
Object.Destroy(gameObject); will remove the gameObject and all components.

about the documentation, if you take a closer look to GameObject page, you’ll notice that it has the Destroy method:
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html
but it’ll lead you to Object.Destroy page, since, as I said before, GameObject inherit Object
http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html

Here’s the new script

var questProgress = "0";

function OnCollisionEnter(){
questProgress = "1";
    if(Input.GetKeyDown ("f")  questProgress == "1"){
       GameObject.Destroy(gameObject);
    }
}

The variable turn to 1 on collision (I changed from trigger to collision) but the object doesn’t destroy.

Hey wi3zap,

var questProgress = "0";
function OnCollisionEnter(Collision : hit){
questProgress = "1";
    if(Input.GetKeyDown (KeyCode.F)  questProgress == "1"){
       GameObject.Destroy(gameObject);
    }
}

GetKeyDown receive a KeyCode, not a String. Also, OnCollisionEnter receive an Collision param, to check witch object you’re colliding (i think it doesn’t matter in you case)

the problem is that OnCollisionEnter and GetKeyDown only happen on 1 frame. That means that you would need to press f at the exact same frame as the collision is happening. Which is very unlikely. I would change OnCollisionEnter to OnCollisionStay

element_wsc is tottaly right.
Tho, this might fix the “bug”

var questProgress = "0";
var isColliding = false;
function OnCollisionEnter(Collision : hit){
	questProgress = "1";
	isColliding = true;
}
function OnCollisionExit(Collision : hit){
	isColliding = false;
}
function Update(){
	if(isColliding){
		if(Input.GetKeyDown (KeyCode.F)  questProgress == "1"){
			GameObject.Destroy(gameObject);
		}
	}
}

That’s what i was thinking. Thanks for the reply. Just one thing that im not understanding: what does “hit” stand for? Unity give me an error

Ups, i made a mistake, its:

(hit : Collision)

hit will be the reference for the object that’s colliding with your gameObject

Everything it’s working fine! Thanks everybody!