Cannot implicitly Convert type error

Asked

Viewed 661 times

0

I’m having a mistake

CS0029, Cannot implicitly Convert type int' toUnityengine.UI.Slider'

How can I fix it?

First Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStats : MonoBehaviour {
    public static int Level = 1;
    public static int nextLevel;
    public static int xp = 0;
    public static int xpToLevel;
    public static int xpDiff;
    public static int life;
    public static int totalLife;
    public static int magic;
    public static int totalMagic;
    public static int attack;
    public static int defense;

    // Use this for initialization
    void Start () {
        recauculation ();

    }

    // Update is called once per frame
    void Update () {
        nextLevel = Level + 1;
        xpToLevel = 50 * nextLevel * Level;

        if (xp >= xpToLevel) {
            xpDiff = xp - xpToLevel;
            LevelUp ();
        }
        attack = 5 * Level;
        defense = 3 * Level;

        if (Input.GetKeyDown (KeyCode.Q)) {
            AddXp (125);
        }
    }
    public void recauculation (){
        totalLife = 25 * Level;
        totalMagic = 10 * Level;
        life = totalLife;
        magic = totalMagic;
    }
    public void AddXp (int newXp) {
        xp += newXp;
    }
    public void LevelUp () {
        Level++;
        xp = 0 + xpDiff;
        recauculation ();
    }
}

According to Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Hud : MonoBehaviour {

    public Text LevelText;
    public Slider lifeSlider;
    public Slider magicSlider;
    public Slider xpSlider;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        LevelText.text = PlayerStats.Level.ToString ();

        lifeSlider = PlayerStats.totalLife;
        lifeSlider = PlayerStats.life;
        magicSlider = PlayerStats.totalMagic;
        magicSlider = PlayerStats.magic;
        xpSlider = PlayerStats.xpToLevel;
        xpSlider = PlayerStats.xp;
    }
}

1 answer

2

The guy UnityEngine.UI.Slider is an object that contains the property Value. You

must set values in this property.

void Update () 
{
     xpSlider.value = Player.totalXp;
}

For more information on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.