Search Unity

Why Is My Script Only Returning "True"?

Discussion in 'Scripting' started by FGPArthurVII, Jun 11, 2018.

  1. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    68
    This script should check if an object (a Fire Extinguisher) is being held, A.K.A Attached to the hand. So it would return true if the objet is or false if it isn't, but for some reason It's only returning "True", even if the object is not there.

    Why?
    How can I fix it?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ExtinguisherJet : MonoBehaviour
    6. {
    7.     private SteamVR_Controller.Device controller;
    8.     public Transform heldExtr;
    9.     private bool lookForExtinguisher ()
    10.     {
    11.         bool isThere = false;
    12.  
    13.         try
    14.         {
    15.             heldExtr = transform.Find("Player/NoSteamVRFallbackObjects/FallbackHand/Extinguisher");
    16.             isThere = true;
    17.         }
    18.         catch (System.Exception e)
    19.         {
    20.             print ("Object Not being Wielded");
    21.             isThere = false;
    22.         }
    23.         return isThere;
    24.     }
    25.  
    26.     void Start()
    27.     {
    28.     }
    29.  
    30.     void Update ()
    31.     {
    32.         print (lookForExtinguisher ());
    33.     }
    34. }
    35.  
     
    Last edited: Jun 11, 2018
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    705
    Find does not throw if it does't get a result, it just returns null.
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    7,231
    This ^^^

    Code (csharp):
    1. heldExtr = transform.Find("Player/NoSteamVRFallbackObjects/FallbackHand/Extinguisher");
    2. if (heldExtr != null)
    3. {
    4.     isThere = true;
    5. }
    6. else
    7. {
    8.     isThere = false;
    9. }
     
  4. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    442
    Or even better: `isThere = heldExtr != null;`

    Whenever you are typing `if(x)y=true;elsey=false` just reduce it to `x=y`
     
  5. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    705
    Meh. Sometimes I leave it the first way because I’m not sure if I’ll add more to the inner scope or not yet. In terms of performance, it should be essentially the same:

    Code (csharp):
    1. cmp ecx edx
    2. je equal
    3. mov [y] 1
    4. jmp after
    5. equal:
    6. mov [y] 0
    7. after:
    vs
    Code (csharp):
    1. cmp ecx edx
    2. mov [y] ZF
    The difference is just two jump instructions, it doesn’t even do any register saves. An optimizing compiler should detect that case and convert it for you anyway.
     
    Last edited: Jun 11, 2018
    Tazadar66 and Joe-Censored like this.
  6. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    442
    Not talking about performance at all, its just readability.

    Sure, if you are adding more it makes sense, but when you arent its just too verbose in my opinion.
     
    Tazadar66 likes this.
unityunity