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
Thanks
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.
@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…).
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.
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