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

Flash Light

Discussion in 'Scripting' started by Feche, Apr 14, 2015.

  1. Feche

    Feche

    Joined:
    Apr 2, 2015
    Posts:
    3
    I have a gameobject called Personaje and a child called flashlight who have a script(turnonof) that turn on and of this light
    I want to create a flashlight object with a trigger so when i pass trough i pick it up and then i can turn on and off my light.

    This is the code i'm using to the Personaje


    void OnTriggerEnter(Collider other){
    if (other.gameObject.tag == "Linterna") {
    Destroy(other.gameObject);

    }
    }

    so when it pass trough the linterna item(the flashlight) is destroid, but i dont know how to call the metod that is inside the personaje child (turnonof) wich mark it like "i got a Flashlight" so i can turn it on and off

    The code of the TurnOnOff is


    public class LightOnOff : MonoBehaviour {
    bool Encendida=false;
    bool Item=false;
    public Light luz;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    if(Input.GetKeyDown("mouse 1")&&(Item==true)){
    Encendida = !Encendida;
    if (Encendida == true){
    luz.enabled=true;
    }
    else{
    luz.enabled=false;
    }
    }
    }

    public void AgarraLinterna(){
    Item = true;
    print ("entro");
    }
    }

    Need Help =)
     
  2. Feche

    Feche

    Joined:
    Apr 2, 2015
    Posts:
    3
    Can i declare ITEM like Static
    and in personaje write
    LightOnOff.Item =true;
    ?
    wich's the disadvantage?
     
  3. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
  4. RabenFutter

    RabenFutter

    Joined:
    Dec 30, 2012
    Posts:
    38
    if the skript with OnTriggerEnter is on the same gameObject with the LightOnOff you can do the following:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other){
    2.         if (other.gameObject.tag == "Linterna") {
    3.             ((LightOnOff )(GetComponent(typeof(LightOnOff)))).Item = true;
    4.             Destroy(other.gameObject);
    5.        
    6.         }
    7.  
    or you could add LightOnOff as an variable and drag the gameobjet wich has LightOnOff on it in inspector:

    Code (CSharp):
    1. public LightOnOff myligthSkript;
    2.     void OnTriggerEnter(Collider other){
    3.         if (other.gameObject.tag == "Linterna") {
    4.             myligthSkript.Item = true;
    5.             Destroy(other.gameObject);
    6.        
    7.         }
    8.  
    also try to write all variable names small in the first letter
     
  5. Feche

    Feche

    Joined:
    Apr 2, 2015
    Posts:
    3
  6. RabenFutter

    RabenFutter

    Joined:
    Dec 30, 2012
    Posts:
    38
    edit:
    nvm you destroy the other gameObject
    like i said you need to store it in a variable