0

I've never had this problem before. I keep getting this error:

error CS0266: Cannot implicitly convert type 'float?' to 'float'. An explicit conversion exists (are you missing a cast?)

Here's my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using LootLocker.Requests;
using TMPro;

public class XPManager : MonoBehaviour
{
    public static XPManager instance;

    string Collect50XP = "Collect50XP";

    public GameObject CollectXpButton;
    public Slider LevelSlider;
    public TMP_Text CurrentLevelTxt, NextLevelTxt, CurrentXpTxt, XpIncreaseAmount;

    private void Awake()
    {
        DontDestroyOnLoad(gameObject);
        if(instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }
    }

    public void Collect50XpEvent()
    {
        LootLockerSDKManager.TriggeringAnEvent(Collect50XP, (response) =>
        {
            if (response.success)
            { 
                Debug.Log("Success");
                CheckLevel();
            }
            else
            {
                Debug.Log("Failed" + response.Error);
            }
        });
    }

    public void CheckLevel()
    {
        LootLockerSDKManager.GetPlayerInfo((response) =>
        {
            CurrentLevelTxt.text = response.level.ToString();
            NextLevelTxt.text = (response.level + 1).ToString();
            CurrentXpTxt.text = response.xp.ToString() + " / " + response.level_thresholds.next.ToString();
            if (LevelSlider.value == LevelSlider.maxValue)
            {
                LevelSlider.maxValue = response.level_thresholds.next - (float)response.xp - int.Parse(XpIncreaseAmount.text);
                LevelSlider.value = 0;                
            }
            else
            {
                LevelSlider.value = (float)response.xp - response.level_thresholds.current;
            }
        });
    }
}

It's coming from this line:

LevelSlider.maxValue = response.level_thresholds.next - (float)response.xp - int.Parse(XpIncreaseAmount.text);
Geeky Quentin
  • 2,469
  • 2
  • 7
  • 28
  • Well, `float?` is nullable so it can either be a float value, or null. A regular `float` can't be null, so it has to have a value. Therefore assigning `float?` (which potentially holds a null value) to a `float` without accounting for the `null` value is impossible. What do you want to do in the situation that it's `null`? – ProgrammingLlama Aug 15 '22 at 02:41
  • P.S. This question is not about using the Visual Studio application, so I've removed your visual-studio tag and replaced it with the C# one as this is about C# code. – ProgrammingLlama Aug 15 '22 at 02:43
  • So the response.xp is pulling from a database. So I’d like the value to be the value of the amount of xp on the users account. Idk what it should be if it’s not that. Should it be 0? – Michael Bailey Aug 15 '22 at 04:49
  • Also, I’m not sure how to implement the article linked to my specific code. Is there anyway you could help me with that? – Michael Bailey Aug 15 '22 at 05:12
  • If you want it to be 0, then you could do `response.xp.GetValueOrDefault()`. Note that if you wanted it to be 50 when it's `null`, for example, you could write `response.xp.GetValueOrDefault(50)`. – ProgrammingLlama Aug 15 '22 at 06:01
  • When I add that I get this error `error CS0266: Cannot implicitly convert type 'int?' to 'float'. An explicit conversion exists (are you missing a cast?)` – Michael Bailey Aug 15 '22 at 06:12
  • Not enough information. Is `response.level_thresholds.next` perhaps an `int?`? Else I'm not sure. Or is `response.xp` perhaps an `int?` not a `float?` ? Without knowing what type `maxValue`, `next`, and `xp` are, it's not really clear. I assume `.text` is `string` – ProgrammingLlama Aug 15 '22 at 06:19
  • I'm not fully sure either. I followed a tutorial on YouTube. However, I was able to get the error to go away by using this code: `LevelSlider.maxValue = response.level_thresholds.next - (float)response.xp??0 - int.Parse(XpIncreaseAmount.text);` but when I try to run the code is gives me a null reference. – Michael Bailey Aug 15 '22 at 06:37
  • Also the `maxValue` is for the max value of a Slider so I believe that's `int?`. `response.level_thresholds.next` is I believe an `int?` as well and the tutorial said that `response.xp` was a supposed to be a `float?`. – Michael Bailey Aug 15 '22 at 06:41
  • 1
    SOLVED! it was some inspector issue. the `??0` worked! – Michael Bailey Aug 15 '22 at 07:02

0 Answers0