2

I am converting my Unity3D game from JS to C#, and I face some problem with this function:

void ReorientationNaming(GameObject g)
    {
        ///find the cube and its bounds
        GameObject tobename = g;
        Bounds cubebound = tobename.renderer.bounds;
        string namex;
        string namey;
        string namez;

        GameObject[] allAxisList = GameObject.FindGameObjectsWithTag("AxisPlain");
        foreach(GameObject allAxis in allAxisList) 
        {
            Bounds axisbound = allAxis.renderer.bounds;
            if (cubebound.Intersects(axisbound))
            {
                if (allAxis.name.Contains("x")) 
                {
                    namex = allAxis.name;
                    namex = namex.Substring(1,1);
                    //print("namex" + namex);
                }
                if (allAxis.name.Contains("y")) 
                {
                    namey = allAxis.name;
                    namey = namey.Substring(1,1);
                }
                if (allAxis.name.Contains("z")) 
                {
                    namez = allAxis.name;
                    namez = namez.Substring(1,1);
                }
            }
            tobename.name = namex+namey+namez;//<-- this line is the problem!
        }
    }

The final line give me the error:

Assets/Cumetry/MainGameLogic.cs(136,41): error CS0165: Use of unassigned local variable `namex'
Assets/Cumetry/MainGameLogic.cs(136,41): error CS0165: Use of unassigned local variable `namey'
Assets/Cumetry/MainGameLogic.cs(136,41): error CS0165: Use of unassigned local variable `namez'

I believe is the way I declare the string. Any idea How can I resolve this?

sooon
  • 4,718
  • 8
  • 63
  • 116
  • The reason this occurs is because it is possible for your string objects to be unassigned (have no value) when they are used on the line `tobename.name = namex+namey+namez;`. Simply assign a blank value when you create them as @DaveDev suggests – Nunners Jan 23 '14 at 11:04

2 Answers2

4

change

    string namex;
    string namey;
    string namez;

to

    string namex = string.Empty;
    string namey = string.Empty;
    string namez = string.Empty;
DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • Wow! quick and spot on! Thanks! Care to explain a bit more in depth?? – sooon Jan 23 '14 at 11:03
  • It's because the default value of `string` is `null`. There are better people who can explain it than me. Check this: http://stackoverflow.com/questions/14337551/why-is-the-default-value-of-the-string-type-null-instead-of-an-empty-string . If the answer is correct, accept it as correct for this and any other answers on StackOverflow that help you. – DaveDev Jan 23 '14 at 11:06
  • Also @sooon notice there is no else in your code to set those vars if none of the previous conditions are true, thus there is the possibility of them being null. – Max von Hippel Aug 07 '15 at 02:21
1

It is because local variables aren't being initialized automatically unlike the instance variables of the object.

Example:

public class Test1
{
    int number; // this gets initialized automatically to zero (0).

    void TestMethod()
    {
        int local; // in C#, you're required to manually initialize it.
    }
}

You might want to check this one for more explanation about it. Why compile error "Use of unassigned local variable"?

Chawy
  • 51
  • 3