Click here to Skip to main content
15,936,247 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
so I made this script but when the player "eats' the "food" the Hunger Slider doesn't increase, it just continues to fall.

script:

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

public class RaycastManager : MonoBehaviour
{
    private GameObject raycastedObj;

    [Header("Raycast Settings")]
    [SerializeField] private float rayLength = 10;
    [SerializeField] private LayerMask newLayerMask;

    [Header("References")]
    [SerializeField] private Image crossHair;
    [SerializeField] private Text itemNameText;

    void Update()
    {
        RaycastHit hit;
        Vector3 fwd = transform.TransformDirection(Vector3.forward);

        if (Physics.Raycast(transform.position, fwd, out hit, rayLength, newLayerMask.value))
        {
            if (hit.collider.CompareTag("Consumable"))
            {
                CrosshairActive();
                raycastedObj = hit.collider.gameObject;
                itemNameText.text = raycastedObj.GetComponent<itemproperties>().itemName;

                if (Input.GetMouseButtonDown(0))
                {
                    raycastedObj.GetComponent<itemproperties>().Interaction();
                    raycastedObj.SetActive(false);
                }
            }
        }

        else
        {
            CrosshairNormal();
            itemNameText.text = null;
        }
    }

    void CrosshairActive()
    {
        crossHair.color = Color.red;
    }

    void CrosshairNormal()
    {
        crossHair.color = Color.white;
    }

    [Header("Your Consumables")]
    public string itemName;

    [SerializeField] private bool food;
    [SerializeField] private bool water;
    [SerializeField] private bool health;
    [SerializeField] private float value;

    [SerializeField] private PlayerStats playerStats;

    public void Interaction()
    {
        if (food)
        {
            playerStats.HungerSlider.value += value;
        }

        else if (water)
        {
            playerStats.ThirstSlider.value += value;
        }

        else if (health)
        {
            playerStats.HealthSlider.value += value;
        }
    }

}


What I have tried:

I've been searching around forums and watching a few YouTube videos but I cant seem to find the answer. any help will be appreciated.
Posted
Updated 8-Mar-21 21:29pm
v2

Michael, you've got to stop this.
THis question is pretty much the same as your How do I make the player heat up?[^] and How do I make my character cool down?[^] questions, and you clearly haven't learned anything from them.

"Searching around forums" and "Youtube videos" are not the way to develop a game - becuas ethey will only let you develop a game that already exists, and where is the point - or fun - in that?
What you need to do is go right back to basics and learn to code properly: develop the mindset with lets you think about problems and how you approach and solve them; as well as the mechanics of how the language works.
Because at the moment, you are flailing around blindly, hoping that it'll all work by magic and that just isn't going to happen. Learning how to do it all properly may seem like effort, but it's directed effort with a specific result. At the moment you are putting loads of effort into avoiding learning and frankly getting nowhere fast - it would be a lot, lot quicker and easier for you in the long run to know what you are doing and how to do it, honest!
Please, consider it, and give it a try?
 
Share this answer
 
Comments
Michael Ciurleo 9-Mar-21 2:42am    
I appreciate your reply and I understand your concern. I have been learning C# and I know a few basics too. However it’s a tricky task and I can’t wrap my head around certain areas like this one, that’s why I ask for help. Once I know the answer I can properly make the object more unique. So i know this is annoying to you but what should I do to fix my issue?
OriginalGriff 9-Mar-21 3:06am    
It's not that it's annoying, it's that "handing you the solution" doesn;t help you at all in the long run - as you have seen from the hot and cold questions.

You really do need to learn the basics and how to think "the development way" and you clearly aren't doing that yet.
I can understand that stuff is boring, and you want to do the exciting stuff like make a game - but without the basics and the right mindset *you* don't produce anything: you bolt together other peoples ideas and hope it all works. And when it doesn't, you are lost with no rudder - you don't know how it works, why it works, so you can't fix it.
And as your game grows, the problem gets worse as the complexity of the "whole code" goes up as you add more ideas from more people that weren't designed to work together. So when that starts to affect other bits of your code you can't react correctly to work out what to do!

Seriously, learn the basics, learn the mindset. You are doing this completely the wrong way round and it will only cause more problems for you, not less!
 
Share this answer
 
Comments
Michael Ciurleo 9-Mar-21 5:25am    
Thanks pal :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900