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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Can't make a sprite be disabled

Discussion in 'Scripting' started by stormtrooperatt, Nov 12, 2020.

  1. stormtrooperatt

    stormtrooperatt

    Joined:
    Jul 23, 2020
    Posts:
    5
    I'm trying to make a black ball become diable/ activate when you press 'e' on it's light.
    but it won't disable at all, nor really come back.
    upload_2020-11-12_14-55-49.png
    here's what it looks like
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class light : MonoBehaviour
    5. {
    6.    public GameObject darkness;
    7.     public bool objactiveon = false;
    8.     bool enter = false;
    9. void start(){
    10. }
    11. //diappear the darkness
    12.   void Update(){
    13.     if (Input.GetKey("e") && enter ){
    14.       Debug.Log("This is a log message.");
    15.              darkness.SetActive(false);}
    16.           }
    17.   // check if playes in the box
    18. void OnTriggerEnter(Collider hit){
    19.       if (hit.CompareTag("Player")){
    20.           Debug.Log("This is a log message.");
    21.          enter = true;
    22.       }}
    23. void OnTriggerExit(Collider hit){
    24.         if (hit.CompareTag("Player")){
    25.            Debug.Log("This is a log message.");
    26.            enter = false;
    27.         }
    28.     }
    29.   }
    here's the code for it
    pls give advice
     

    Attached Files:

    Last edited: Nov 14, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    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 are the values of the variables involved? Are they initialized?

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

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Everything @Kurt-Dekker said is true.

    That being said, if your text is an actual copy of your code, then it appears your update is not capitalized. update should be Update.
     
    Kurt-Dekker and adehm like this.
  4. stormtrooperatt

    stormtrooperatt

    Joined:
    Jul 23, 2020
    Posts:
    5
    ok, I put the debug logs in and found that update is working and noathing else. I'll go get pics of the related objects in case there's something wrong with them
    edit; also the bool isn't working as well Screenshot (8).png Screenshot (9).png Screenshot (7).png
     
    Last edited: Nov 14, 2020
  5. stormtrooperatt

    stormtrooperatt

    Joined:
    Jul 23, 2020
    Posts:
    5
    figured it out, I didn't say it was a 2D collider in the code