Search Unity

Help Wanted Problem with UI Script

Discussion in 'Scripting' started by Palmtendo, Jun 30, 2021.

  1. Palmtendo


    Dec 22, 2020
    Hello everyone,
    in my current script is a problem that activates when you have 2 or more objects activated at a time.

    The way my script works it should display the power drain. For example I have one object that drains power activated and the UI displays "very low". If I have 2 objects that drain power activated the UI should display "low", but what it does it displays "very low" and "low" at the same time. And when I activate 3 objects it displays nothing.

    There is probably an error in my current script, but I don't know where.

    To sum up:
    - it should display a standard text in the beginning something like "very low"
    - when I activate an object it should display "low" and when I deactivate this object it should go back to "very low"
    - when I have multiple objects activated the text should display it like: 3 objects = medium, 4 objects = high and so on

    To get a better understanding of what I mean, it is basically like the power mechanic in FNAF.
    You can see in the down left corner "Usage" and then a bar that increases the more you have activated.
    Example video on youtube:

    I tried to do exactly the same, but not in form of bars rather in form of a text.

    Here is my script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class PowerUsageGraphic : MonoBehaviour
    6. {
    7.     public GameObject[] texts;
    9.     void Start()
    10.     {
    11.         for(int i = 0; i < texts.Length; i++)
    12.         {
    13.             texts[i].SetActive(false);
    14.         }
    15.     }
    17.     public void SetBars(int numberOfBars)
    18.     {
    19.         if (numberOfBars < 0)
    20.             numberOfBars = 0;
    22.         if(numberOfBars >= texts.Length)
    23.         {
    24.             numberOfBars = texts.Length - 1;
    25.         }
    27.         for(int i = 0; i < texts.Length; i++)
    28.         {
    29.             texts[i].SetActive(false);
    30.         }
    32.         for (int i = 0; i < numberOfBars; i++)
    33.         {
    34.             texts[i].SetActive(true);
    35.         }
    36.     }
    37. }
    Hit me up if you need more information and thanks in advance.
    Last edited: Jun 30, 2021
  2. Brathnann


    Aug 12, 2014
    I'm not really understanding your explanation when you say doesn't work or works fine. Since I'm unsure of what your code is tied to.

    If you have PowerUsageGraphic script on multiple objects and the GameObject array is tied to the same objects, then when you turn the PowerUsageGraphic objects on, their Starts run which sets everything to inactive in the array.

    Which based on your description of "displays nothing" makes me think you have scripts overwriting each other.

    Can you better explain what you want to happen and what is actually happening? Also, where does this script exist in your scene? One object? Multiple objects?
    Palmtendo likes this.
  3. Palmtendo


    Dec 22, 2020
    Well thanks for trying to help me. I made the post more clear now and I hope you now know what I mean.
  4. Kurt-Dekker


    Mar 16, 2013
    There may be, or as Brath pointed out, it might just be misconfigured, duplicated, improperly set up, etc..

    Either way, you have to find out exactly what is going on right now in its broken state before you can begin to fix it.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
  5. Brathnann


    Aug 12, 2014
    So does PowerUsageGraphic only exist once in your scene?

    Honestly, I would personally change up your script. I would have a single script that manages the bars, has a reference to a text object, etc. Something like PowerManager.

    Then, I would have other scripts that when they effect the power level, they send over a plus or minus. So, in the case of like FNAF, I think closing a door drains power faster or whatever and opening it reduces power use.

    So, when the door is closed, it sends over a plus. The PowerManager tracks that it now has 1 item draining power. It then changes some text to read "Low".

    If a second door is closed, it sends over a plus again. The PowerManager now has a value of 2 in an int and knows there are two items draining power. Now it swaps the text to say "Very Low".

    Note this only uses one text object and you can also make adjustments to some battery gui or what not.

    If the door is opened, it sends over a minus and the value can swap back to "Low".

    This would require a rewrite of your systems, but I think it would be worth it. Otherwise, your explanation helped, but if you're doing what I think you are, I do think it's a multi script conflict.

    Edit: Also note @Kurt-Dekker post gives you a lot of helpful info on how to Debug your code and is worth noting, even if you do decide to change up your scripts.
  6. Palmtendo


    Dec 22, 2020
    Thanks for the tips.

    I did it in a similar way with the Power Manager.

    How I can do this when I have a value of 1 that I can display a text and that I display a different text when the value increases. Like you explained in your answer.
    This would be really helpful and would probably solve my problem.

    EDIT: I managed to find a solution with your help where I can delete the complete script.

    Its definitely not perfect, but its working and thats fine for now.

    in Update:
    Code (CSharp):
    1. if (Charge > 0)
    2.         {
    3.             if (usage == 0)
    4.             {
    5.                 text.text = "Usage: very low";
    6.             }
    8.             if (usage == 1)
    9.             {
    10.                 text.text = "Usage: low";
    11.             }
    13.             if (usage == 2)
    14.             {
    15.                 text.text = "Usage: moderate";
    16.             }
    18.             if (usage == 3)
    19.             {
    20.                 text.text = "Usage: high";
    21.             }
    23.             if (usage == 4)
    24.             {
    25.                 text.text = "Usage: very high";
    26.             }
    28.             if (usage == 5)
    29.             {
    30.                 text.text = "Usage: extreme";
    31.             }
    32.         }
    33.         else
    34.         {
    35.             Destroy(text);
    36.         }
    Last edited: Jun 30, 2021