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. Dismiss Notice

ONTriggerStay i cant change varible boolean "a" by second mouse click(0)

Discussion in 'Scripting' started by like_a_boss, Mar 10, 2015.

?

why doest it work?

Poll closed Mar 17, 2015.
  1. couse it should be done in update

    0 vote(s)
    0.0%
  2. other reason

    0 vote(s)
    0.0%
  1. like_a_boss

    like_a_boss

    Joined:
    Jan 17, 2015
    Posts:
    15
    HI i have a simple code but i have a prolbem with boolean value
    varible boolean "a" isn't'being changed on second Mouse(0) click. The result is that objects are desactivated but i can not activate them again.
    Maybe someone could ytake a quick look

    Code (JavaScript):
    1.    
    2. function OnTriggerStay(col:Collider){
    3. if(col.tag=="Player"){
    4.  
    5.     if(Input.GetMouseButtonDown(0)&& a==true){a=false;
    6.         obiekt1.active=true;
    7.         obiekt2.active=true;}
    8.     if(Input.GetMouseButtonDown(0)&& a==false){a=true;
    9.         obiekt1.active=false;
    10.         obiekt2.active=false;}
    11.                     }
    12. }          
    13.  
    14.  
     
  2. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    Line 5 missing something in the if statement Input.GetMouseButtonDown(0)a==true. Its && or ||?
     
  3. like_a_boss

    like_a_boss

    Joined:
    Jan 17, 2015
    Posts:
    15
    &&was missed but it doesn't change the behavior.
    "a" varible turns true on first mouse click but doest want to change to false on second mouse click
     
  4. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    Try this, and let me know if it works

    Code (JavaScript):
    1.  
    2. function OnTriggerStay(col:Collider){
    3. if(col.tag=="Player"){
    4.     if(Input.GetMouseButtonDown(0)){
    5.         obiekt1.active = a;
    6.         obiekt2.active = a;
    7.         a = !a;
    8.     }
    9. }
    10.  
    Also
     
  5. like_a_boss

    like_a_boss

    Joined:
    Jan 17, 2015
    Posts:
    15
    this one works.but why the first one didn't? because all instructions have been done in one frame?
     
  6. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    Most likely.
    The API says that GetMouseButtonDown must be on Update. You should debug line by line, and there you'll see what's happening.

    Or put a log to see the return value of GetMouseButtonDown.
     
  7. like_a_boss

    like_a_boss

    Joined:
    Jan 17, 2015
    Posts:
    15
    thx a lot how can i repay you? i can send you simbolic 1$ via paypall XD
     
  8. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    No need for payment. A thankee is enought ;)
     
  9. like_a_boss

    like_a_boss

    Joined:
    Jan 17, 2015
    Posts:
    15
    thank you XD!