-1
\$\begingroup\$

I followed the Brackeys' How to make a health bar tutorial on YouTube, and it worked perfectly when playing around with the sliders myself. But, when I tried to drag the health bar object into the script's exposed attribute in the Inspector (like Brackeys does at 10:14 in the video), but it won't let me drag it in. I followed the tutorial exactly as shown, but it still won't work. Can anyone explain why and/or how to fix it?

The script for the player stamina:

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

public class PlayerStamina : MonoBehaviour
{
    public int maxPlayerStamina = 100;
    public int currentPlayerStamina;

    public int leftMouseStaminaDrain = 20;
    public int rightMouseStaminaDrain = 35;

    public StaminaBar staminaBar;

    // Start is called before the first frame update
    void Start()
    {
        currentPlayerStamina = maxPlayerStamina;
        staminaBar.SetMaxStamina(maxPlayerStamina);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.G))
        {
            LoseStamina(20);
        }
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            LoseStamina(leftMouseStaminaDrain);
        }
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            LoseStamina(rightMouseStaminaDrain);
        }
    }

    void LoseStamina(int staminaDrain)
    {
        currentPlayerStamina -= staminaDrain;

        staminaBar.SetStamina(currentPlayerStamina);
    }
}

The script for the stamina slider:

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

public class StaminaBar : MonoBehaviour
{
    public Slider slider;

    public void SetMaxStamina(int stamina)
    {
        slider.maxValue = stamina;
        slider.value = stamina;
    }

    public void SetStamina(int stamina)
    {
        slider.value = stamina;
    }
}

enter image description here

enter image description here

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ Assume you're talking to someone who has never seen this video. Explain step by step what we need to do to create a Minimal Complete Verifiable Example of this problem. Include screenshots if it helps you explain where you're stuck. Remember we can't see your screen, so we don't know what you're dragging, where you're dragging it from, or where you're dragging it to, unless you show us or describe it in detail. \$\endgroup\$
    – DMGregory
    Nov 12 '21 at 23:59
  • \$\begingroup\$ When you crop your screenshots so tight like this, we can only see that there is an object called StaminaBar — we can't see where it is. Same goes for the Player Stamina script snippet. We can't tell whether that's on an object in the same scene as the StaminaBar, or in a prefab in your Assets folder, etc. \$\endgroup\$
    – DMGregory
    Nov 13 '21 at 12:28
0
\$\begingroup\$

You have a GameObject called StaminaBar, but you are not showing if that GameObject has the StaminaBar Component attached to it. Please double-check if you actually dragged the StaminaBar script to your StaminaBar GameObject inside the Inspector, and try again to drag the GameObject to the field exposed by your GameObject containing the PlayerStamina class. This is mandatory because the script is looking for a GameObject with a StaminaBar component attached to it, otherwise it won't let you perform the drag & drop.

\$\endgroup\$
0
\$\begingroup\$

Maybe try using the circle button beside the bar where we put in the slider and choose it. Also it would be nice if you could post the code you used for the StaminaBar class, as there might be an issue there.

\$\endgroup\$
1
  • \$\begingroup\$ The StaminaBar code is present in the question. \$\endgroup\$
    – DMGregory
    Nov 13 '21 at 3:49
0
\$\begingroup\$

Your PlayerStamina script exposes a field of type StaminaBar, meaning that you can assign any GameObject with a StaminaBar Component to it. But, you cannot assign a GameObject that doesn't have such a Component, even if one of its children has it instead.

In order to drag the StaminaBar GameObject into the exposed field, make sure StaminaBar does have the StaminaBar Component itself. I try to guess that StaminaBar is an empty GameObject that contains the actual GameObject with a StaminaBar Component. This setup may be desirable for ad-hoc UI displacement with anchors. In this case, try and drag the child GameObject in the exposed field in the Inspector instead of its parent.

\$\endgroup\$
1
  • \$\begingroup\$ This was it, thank you! \$\endgroup\$
    – Dalsha
    Nov 13 '21 at 15:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .