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?

1
  • 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 Commented Jan 23, 2014 at 11:04

2 Answers 2

4

change

    string namex;
    string namey;
    string namez;

to

    string namex = string.Empty;
    string namey = string.Empty;
    string namez = string.Empty;
Sign up to request clarification or add additional context in comments.

3 Comments

Wow! quick and spot on! Thanks! Care to explain a bit more in depth??
It's because the default value of string is null. There are better people who can explain it than me. Check this: stackoverflow.com/questions/14337551/… . If the answer is correct, accept it as correct for this and any other answers on StackOverflow that help you.
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.
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"?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.