Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Help with a script

Discussion in 'Scripting' started by stocksfieldhero, Apr 15, 2023.

  1. stocksfieldhero

    stocksfieldhero

    Joined:
    Jan 18, 2023
    Posts:
    2
    I would really appreciate some help with altering this code so that it will turn off all the lights in the game rather than turn on. Im very new to this and I have tried changing bits around but I can't get it to work. Essentially when the player is in reach I want them to hit the interact button and that will trigger all the lights attached to this script to turn off.

    Thank you in advance :)


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class TurnPowerOn : MonoBehaviour
    7. {
    8.     public GameObject[] lights;
    9.  
    10.     public GameObject text;
    11.  
    12.     public bool powerIsOn;
    13.  
    14.     private bool inReach;
    15.  
    16.     void Start()
    17.     {
    18.  
    19.         foreach (GameObject ob in lights)
    20.         {
    21.             ob.SetActive(false);
    22.         }
    23.  
    24.         text.SetActive(false);
    25.  
    26.     }
    27.  
    28.     void OnTriggerEnter(Collider other)
    29.     {
    30.         if (other.gameObject.tag == "Reach" && !powerIsOn)
    31.         {
    32.             inReach = true;
    33.             text.SetActive(true);
    34.         }
    35.  
    36.     }
    37.  
    38.     void OnTriggerExit(Collider other)
    39.     {
    40.         if (other.gameObject.tag == "Reach")
    41.         {
    42.             inReach = false;
    43.             text.SetActive(false);
    44.         }
    45.     }
    46.  
    47.     void Update()
    48.     {
    49.         if (Input.GetButtonDown("Interact") && inReach)
    50.         {
    51.             powerIsOn = true;
    52.         }
    53.  
    54.         if(powerIsOn)
    55.         {
    56.             foreach (GameObject ob in lights)
    57.             {
    58.                 ob.SetActive(true);
    59.             }
    60.  
    61.             inReach = false;
    62.             text.SetActive(false);
    63.         }
    64.  
    65.         if (!powerIsOn)
    66.         {
    67.             foreach (GameObject ob in lights)
    68.             {
    69.                 ob.SetActive(false);
    70.             }
    71.  
    72.         }
    73.  
    74.     }
    75. }
    76.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    Update seems pretty straight forward. powerIsOn is only being set to true though, so you need to set it to false when it's true in order of the other if condition to work.

    Usually that's as simple as

    Code (CSharp):
    1. powerIsOn = !powerIsOn;
    This would always set it to the opposite value of itself, essentially toggling it between true/false.
    Now, the other issue is the inReach value. Probably don't want to set it to false in Update. Your trigger calls should handle this just fine. That should get you heading in the right direction if it's not enough to solve it.
     
    Olipool likes this.
  3. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    In addition to what Brathnann suggested, I would also remove
    && !powerIsOn

    from line 30. The method should set inReach no matter if the power is on or not. It should just determine if you are next to the interaction.
     
    Brathnann likes this.
  4. stocksfieldhero

    stocksfieldhero

    Joined:
    Jan 18, 2023
    Posts:
    2
    Thank you Brathnann and OliPool! I have got it working, although I don't get text to pop up but that fine I can make that work.

    Thank you so much
     
    Olipool likes this.