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

Checking if a variable has a value

Discussion in 'Scripting' started by Shadowing, Dec 1, 2015.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    I thought I've done this before with no issue.
    Compile error Use of unassigned local variable `SelectedShip
    Trying to check if Selectedship has a value
    ive also tried
    if(SelectedShip){}

    Code (csharp):
    1.  
    2. if(SelectedShip != null) {
    3.  
    4. SelectedShip.SetActive(true);
    5.  
    6. }
    7.  

    Code (csharp):
    1.  
    2. publicvoid OnClicked(Button button) {
    3.  
    4. GameObject SelectedShip;
    5.  
    6. foreach(GameObject child in ShipWrapperTransform) {
    7.  
    8.   if (child.name == button.name){
    9.  
    10.      SelectedShip = child;
    11.  
    12.    }else{
    13.  
    14.    }
    15.  
    16. }
    17.  
    18. if(SelectedShip != null) {
    19.  
    20.    SelectedShip.SetActive(true);
    21.  
    22. }
    23.  
    24. }
    25.  
    26.  
     
  2. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    I just realized I can just assign null to it to fix it.
    GameObject SelectedShip = null;

    Still seems like there should be a way to check though.
     
    axelchavezj likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    it's a c# thing... the code above that check has the option for selectedship to never be assigned anything (i.e. the else clause).


    edit: it'll work the way you are expecting it to if the declaration is outside the function

    i.e.

    Code (csharp):
    1.  
    2. public class someclass : ...
    3. {
    4.     GameObject myobj;
    5.  
    6.     public void TestFunction()
    7.     {
    8.         if(myobj)
    9.         {
    10.  
    11.         }
    12.    }
    13. }
    14.  
     
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    like the error says it's a local unassigned. As soon as you take out the GameObject SelectedShip to the class instead of the method. it will work without setting it explizit to null.

    I think it's because if you have a variable in the class not assigned with a value, it's automatically set to it's default values.
    GameObject, Lists, Arrays -> null
    int float -> 0
    Vector -> Vecter.Zero
    bool -> false

    as soon you have this declaration in an method it's not recognized and therefore the error.

    Code (CSharp):
    1. GameObject SelectedShip;
    2.  
    3. publicvoid OnClicked(Button button) {
    4.    foreach(GameObject child in ShipWrapperTransform) {
    5.       if (child.name == button.name){
    6.          SelectedShip = child;
    7.       }else{
    8.       }
    9.    }
    10.  
    11.    if(SelectedShip != null) {
    12.       SelectedShip.SetActive(true);
    13.    }
    14. }
     
  5. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    will nothing change to the error if you set SelectedShip in the else, tried it out :)
     
  6. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,628
    Ahh ya lol.
    Ya I come from a strong PHP background. I never used classes with PHP.

    Thanks guys I appreciate it. I was like what the heck for half an hour on that lol.
    Actually I think I knew this but forgot. I was away from Unity for 3 months then just came back again to work on my project and kind of forgot some stuff.

    It fades pretty fast apparently lol.
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    hmm good point, there might be no execution of the foreach i.e. no children of the transform... :p
     
  8. TromJH230

    TromJH230

    Joined:
    Feb 19, 2020
    Posts:
    2
  9. Shreddedcoconut

    Shreddedcoconut

    Joined:
    Jul 30, 2021
    Posts:
    61