Search Unity

Making a power/battery system unity 5.61

Discussion in 'Scripting' started by FreddieM, Aug 27, 2018.

  1. FreddieM

    FreddieM

    Joined:
    Jun 29, 2017
    Posts:
    4
    I want to start off by saying that I am trying to make a Five Nights At Freddy's fan game in unity 3D. I have got to the point in which I want to increment the power usage into the game. I am pretty new to coding and may still be making simple (and large) mistakes. I have got the fundamentals of how I want to do this and I have made some code that I thought would work... it didn't. Here is the code:


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

    public class PowerV2 : MonoBehaviour {

    public TextMesh powertext;

    public GameObject LeftLight;
    public GameObject RightLight;


    public int maxPower;
    public int minPower;




    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {
    StartCoroutine (lightsCheck ());
    powertext.text = maxPower.ToString ();
    }

    IEnumerator lightsCheck()
    {
    if (!LeftLight.activeInHierarchy)
    maxPower = maxPower - 1;
    powertext.text = maxPower.ToString ();
    yield return new WaitForSeconds (2);
    }

    }





    I dragged each game object into the right box (The left light game object was dragged into the LeftLight public game object, etc...) When I run the scene after 2 seconds it removes 1% even when the left light is not even active yet, then it continues to minus 1 percent every frame. I would like to thank you in advance for helping me understand code more!
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    !LeftLight.activeInHierarchy

    This says, if the LeftLight is not active, subtract 1. Remove the ! if you want it to check if it is active.
    Also, not sure what reason you are doing the check in a coroutine? Since you're calling it from Update to begin with, it's going to start the coroutine every frame. Which means each frame it hits will subtract 1.

    Also, use code tags when you do code post to make things easier to read.
     
    FreddieM likes this.
  3. FreddieM

    FreddieM

    Joined:
    Jun 29, 2017
    Posts:
    4
    Ok thank you very much, I am pretty much brand new to coding so I make so many mistakes, thank you for the corrections and advice I will be sure to use them, I have one error though which is
    "The body of `PowerV2.Update()' cannot be an iterator block because `void' is not an iterator interface type".

    Thank you very much you have been very helpful!
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Are you trying to yield in Update? That isn't allowed. If you want it to only tick every x seconds, you could do a coroutine that gets called only when you turn on the light instead of trying to check if a light is active every frame. Then kill the coroutine if the light is turned off.