Netanel
Netanel

Reputation: 78

Unity error in the build that didn't happend in the editor

I tested my game in the build, But I got an error that I didn't get in the editor Here is the build image: https://netane54544-gmail.tinytake.com/media/7608c0?filename=1525514080068_05-05-2018-12-52-30.png&sub_type=thumbnail_preview&type=attachment&width=1198&height=654

And here is a image from the editor: https://netane54544-gmail.tinytake.com/media/7608cc?filename=1525514464717_05-05-2018-12-58-59.png&sub_type=thumbnail_preview&type=attachment&width=1198&height=654

Also here is the button code:

private void Clicked()
{
    int index = 0;

    foreach (Gun item in gunSystem.secInventory)
    {
        Debug.Log(item.gunType);
        if (index == ButtonId)
        {
            if (gunSystem.guns_inInventory[playerScript.keyPress] == false)
            {
                gunSystem.setGunInvetory(playerScript.keyPress, item.Index.ToString());
                Worked = true;
            }
        }
        else
        {
            index++;
        }

    }
}

And setGunInvetory code:

public void setGunInvetory(int n, string gunName)
{
    foreach (Gun item in secInventory)
    {
        Debug.Log(item.gunType);
        if (item.gunType == "Normal" && guns_inInventory[playerScript.keyPress] == false)
        {
            playerGuns[playerScript.keyPress] = Instantiate(Weapon_Normal, Camera.main.transform.position + (Camera.main.transform.forward * Normalview.offSetPosition.z) + (Camera.main.transform.right * Normalview.offSetPosition.x) + (Camera.main.transform.up * Normalview.offSetPosition.y), Quaternion.identity) as GameObject;
            playerGuns[playerScript.keyPress].name = gunName;
            playerGuns[playerScript.keyPress].transform.parent = gameObject.transform;
            playerGuns[playerScript.keyPress].transform.eulerAngles = gameObject.transform.eulerAngles + Normalview.startOffsetRotation;
            setGunActive(playerScript.keyPress);

            //Store gundata in inventory
            item.Named = false;
            Inventory[playerScript.keyPress] = item;
            guns_inInventory[playerScript.keyPress] = true;
        }
        else if (item.gunType == "Stride" && guns_inInventory[playerScript.keyPress] == false)
        {
            playerGuns[playerScript.keyPress] = Instantiate(Weapon_Stride, Camera.main.transform.position + (Camera.main.transform.forward * Strideview.offSetPosition.z) + (Camera.main.transform.right * Strideview.offSetPosition.x) + (Camera.main.transform.up * Strideview.offSetPosition.y), Quaternion.identity) as GameObject;
            playerGuns[playerScript.keyPress].name = gunName;
            playerGuns[playerScript.keyPress].transform.parent = gameObject.transform;
            playerGuns[playerScript.keyPress].transform.eulerAngles = gameObject.transform.eulerAngles + Strideview.startOffsetRotation;
            setGunActive(playerScript.keyPress);
            playerGuns[playerScript.keyPress].GetComponent<Renderer>().material = Stride_Material;

            //Store gundata in inventory
            item.Named = false;
            Inventory[playerScript.keyPress] = item;
            guns_inInventory[playerScript.keyPress] = true;
        }
    }
}

Upvotes: 0

Views: 44

Answers (1)

Swift
Swift

Reputation: 3410

OP mentioned in the comments that the problem lies on this line

playerGuns[playerScript.keyPress] = Instantiate(Weapon_Normal, Camera.main.transform.position + (Camera.main.transform.forward * Normalview.offSetPosition.z) + (Camera.main.transform.right * Normalview.offSetPosition.x) + (Camera.main.transform.up * Normalview.offSetPosition.y), Quaternion.identity) as GameObject;

which brings the first culprit to playerGuns. The NullReferenceException occured because playerGuns dictionary was only declared and not initialized with new Dictionary<TKey, TValue>() and hence remains null instead of a initialized empty dictionary.

The solution here is to create a new instance of the dictionary before using it:

Dictionary<TKey,TValue> playerGuns = new Dictionary<TKey, TValue>();

Upvotes: 1

Related Questions