Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help, is this right?

Discussion in 'Scripting' started by wi3zap, Apr 29, 2014.

  1. wi3zap

    wi3zap

    Joined:
    Apr 26, 2014
    Posts:
    6
    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
    Code (csharp):
    1.  
    2. var questProgress = "0";
    3.  
    4. function OnTriggerEnter(){
    5. questProgress = "1";
    6.     if(Input.GetKeyDown ("f")  questProgress == 1){
    7.        Destroy(this.gameObject);
    8. }
    9. }
    10.  
    But the object doesn't destroy. I guess i made an error :confused:
    Thanks
     
  2. Mukabr

    Mukabr

    Joined:
    Jun 10, 2013
    Posts:
    61
    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:
    Code (csharp):
    1. function OnTriggerEnter(Collider : hit){
    2.     if(hit.transform.tag == "objtag"){
    3.         //dostuff  
    4.     }
    5. }
    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 :

    Code (csharp):
    1.  GameObject.Destroy(gameObject)
    Another mistake you made in the if condition was to use only one , the right form:

    Code (csharp):
    1. if(Input.GetKeyDown ("f")  questProgress == 1){
     
    Last edited: Apr 29, 2014
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
    Last edited: Apr 29, 2014
  5. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Take out the quotes. Quotes denote a string, while you want to compare integers. You should look at data types.
     
  6. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    @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...).
     
  7. wi3zap

    wi3zap

    Joined:
    Apr 26, 2014
    Posts:
    6
    Thank everybody. BTW i copied the wrong code, now it's fixed.
    Thanks
     
  8. Mukabr

    Mukabr

    Joined:
    Jun 10, 2013
    Posts:
    61
    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 :

    Code (csharp):
    1.  Object.Destroy(gameObject.GetComponent<gameObject.GetComponent<MyScript>());
    this will remove the script MyScript from gameObject, and
    Code (csharp):
    1.  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
     
  9. wi3zap

    wi3zap

    Joined:
    Apr 26, 2014
    Posts:
    6
    Here's the new script
    Code (csharp):
    1.  
    2. var questProgress = "0";
    3.  
    4. function OnCollisionEnter(){
    5. questProgress = "1";
    6.     if(Input.GetKeyDown ("f")  questProgress == "1"){
    7.        GameObject.Destroy(gameObject);
    8.     }
    9. }
    10.  
    The variable turn to 1 on collision (I changed from trigger to collision) but the object doesn't destroy.
     
  10. Mukabr

    Mukabr

    Joined:
    Jun 10, 2013
    Posts:
    61
    Hey wi3zap,

    Code (csharp):
    1. var questProgress = "0";
    2. function OnCollisionEnter(Collision : hit){
    3. questProgress = "1";
    4.     if(Input.GetKeyDown (KeyCode.F)  questProgress == "1"){
    5.        GameObject.Destroy(gameObject);
    6.     }
    7. }
    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)
     
  11. Deleted User

    Deleted User

    Guest

    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
     
  12. Mukabr

    Mukabr

    Joined:
    Jun 10, 2013
    Posts:
    61
    element_wsc is tottaly right.
    Tho, this might fix the "bug"

    Code (csharp):
    1. var questProgress = "0";
    2. var isColliding = false;
    3. function OnCollisionEnter(Collision : hit){
    4.     questProgress = "1";
    5.     isColliding = true;
    6. }
    7. function OnCollisionExit(Collision : hit){
    8.     isColliding = false;
    9. }
    10. function Update(){
    11.     if(isColliding){
    12.         if(Input.GetKeyDown (KeyCode.F)  questProgress == "1"){
    13.             GameObject.Destroy(gameObject);
    14.         }
    15.     }
    16. }
     
  13. wi3zap

    wi3zap

    Joined:
    Apr 26, 2014
    Posts:
    6
    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
     
  14. Mukabr

    Mukabr

    Joined:
    Jun 10, 2013
    Posts:
    61
    Ups, i made a mistake, its:

    Code (csharp):
    1. (hit : Collision)
    hit will be the reference for the object that's colliding with your gameObject
     
  15. wi3zap

    wi3zap

    Joined:
    Apr 26, 2014
    Posts:
    6
    Everything it's working fine! Thanks everybody!