Search Unity

Keep getting an error for code that works. And I can't figure out why?

Discussion in 'Scripting' started by GeekStories, Sep 2, 2015.

  1. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    So this is line 55 here
    Code (CSharp):
    1. LettersInv.text += lettersInv[i].ToString();
    And this is line 35 here
    Code (CSharp):
    1. updateInv();
    I have assigned the LettersInv.
    Code (CSharp):
    1. public Text LettersInv;
    this is done in the editor.
    And the lettersInv variable is never empty.
    This is in the Start function
    Code (CSharp):
    1.  
    2.         for(int i = 0; i<3; i++)
    3.         {
    4.             int c = Random.Range (0, Alphabet.Length - 1);
    5.             lettersInv[i] = Alphabet[c];
    6.             Debug.Log(lettersInv[i]);
    7.         }
    8.  
    Any help on getting rid of this error? thanks!
     
    Last edited: Sep 2, 2015
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Since updateInv() is being called from within Update and you're saying it does still do what it's supposed to, I'm assuming what's happening is that updateInv is actually being called before the value of LettersInv is assigned.

    Do you get that error only once, or multiple times?
     
  3. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    UpdateInv is indeed being called from the Update but its inside a if statement so it only runs when LettersInv length is LESS than lettersInv.length.

    This is the if statement inside the Update() function
    Code (CSharp):
    1.         if(LettersInv.text.Length < lettersInv.Length)
    2.         {
    3.             updateInv();
    4.         }
     
  4. Lorenei

    Lorenei

    Joined:
    Aug 8, 2015
    Posts:
    8
    I'm not sure since I'm kind of beginner, but I think you could check this line:
    Code (CSharp):
    1. int c = Random.Range (0, Alphabet.Length - 1);
    Random.Range returns a value from min to max, but never max, so I think you could be missing one reference since it will never return last item from Alphabet array. I'm probably wrong, but maybe
    Code (CSharp):
    1. int c = Random.Range(0, Alphabet.Length);
    would work?
     
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Check what is null on that line.
    Code (csharp):
    1.  
    2. if (LettersInv == null) Debug.Log("LettersInv == null");
    3. if (LettersInv.text == null) Debug.Log("LettersInv.text == null");
    4. if (lettersInv == null) Debug.Log("lettersInv == null");
    5. if (lettersInv[i] == null) Debug.Log("lettersInv[i] == null");
    6. LettersInv.text += lettersInv[i].ToString();
    7.