Search Unity

OnMouseDown() & OnMouseUp()

Discussion in 'Scripting' started by Deleted User, Aug 14, 2014.

  1. Deleted User

    Deleted User

    Guest

    Hello Everyone,

    I have a scenario where player clicks on one of the objects and the respective boolean variable selected becomes true. My issue is, when the player clicks on another object of same class, which spawns a sec later, Its respective boolean selected also becomes true but the previous object boolean selected also remains true but I want only one boolean to be true at a given time. I tried OnMouseUp() & OnMouseUpAsButton() but no luck. Please help. Thanks

    Regards,
    Bhanu
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Could you post your code?
     
  3. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    When you select something, save the object selected. When you select another one, deselect the old one. That's the theory... :)
     
    Magiichan and angrypenguin like this.
  4. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    code???
     
  5. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Exactly what @Fraconte said, if you don't know how to so that, then please share your code with us.
     
  6. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    code = program, script
     
  7. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    I think he was asking for the code...
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well, this has been a good thread everybody. Champagne all around!
     
  9. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Ops! Sorry... I was reading StarManta asking for code and thought "Code???" was the answer of Bhanu3d. :)
     
  10. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    i am askng for ur code used in the script man
     
  11. Hoffers

    Hoffers

    Joined:
    Aug 1, 2014
    Posts:
    3
    You could have something in the update() function like so:

    Code (CSharp):
    1.  
    2.  
    3. if(firstBool == true)
    4. {
    5.     secondBool = false;
    6. }
    7. else
    8. {
    9.  
    10.   firstBool = true;
    11.   secondBool = false;
    12.  
    13. }
    14.  
    I have no idea if this would work. I am only learning C# through trial and error.. In theory it should work..
     
  12. Deleted User

    Deleted User

    Guest

    Hello Guys,

    I am sorry for the late reply but the code is as follows.

    void OnMouseDown()
    {
    selected = true;
    }

    Now, that script is attached to enemy object and enemies spawn continuously. What I want is, if enemy1 selected, selected of that particular enemy becomes true leaving rest false which is perfect but when I select enemy2, its selected becomes true as well but I want the enemy1 selected to be back to false again making only selected enemy's bool variable true but the rest false. Hope this helps. Thanks

    Regards,
    Bhanu
     
  13. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Try this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SelectOne : MonoBehaviour
    5. {
    6.     static public SelectOne Current;
    7.     public bool Selected = false;
    8.  
    9.     void OnMouseDown()
    10.     {
    11.         if (Current != null)
    12.             Current.Selected = false;
    13.         Selected = true;
    14.         Current = this;
    15.     }
    16. }
    17.  
     
    image28 likes this.
  14. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    i thought Fraconte s right
     
  15. Deleted User

    Deleted User

    Guest

    Hello Guys,

    Both of you were right. It worked perfectly and I thank you guys very much.

    Regards,
    Bhanu
     
    Fraconte likes this.
  16. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    cool.always welcome
     
  17. Deleted User

    Deleted User

    Guest

    Hello Guys,

    I am tying to spawn a gameobject every 5 secs and the gameobject should spawn in 5 different x positions randomly and only between a specified 5 positions and I tried Random.range (5 , 25) but thats not what I am looking for. Basically, I want to spawn in 5 , 10 , 15 , 20 , 25 x positions randomly choosing one of those 5 but not between 5 & 25. Please help. Thanks

    Regards,
    Bhanu
     
  18. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Use: Random.range (1, 5) * 5
     
  19. Deleted User

    Deleted User

    Guest

    Doesn't this give me the same unwanted result? The Random.range chooses any value between 5 & 25, take 9 , 11 , 17, etc for example but I don't want any of these values. I hope my problem is more clear now. Thanks
     
  20. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    The random range chose any value between 1 & 5. I think you do better just trying for yourself, because it's only one line of code...
    Edit: Indeed I was thinking about INT not FLOAT range. I just discovered that the INT range is exclusive so you have to do:
    Random.range (1, 6) * 5
     
    Last edited: Sep 2, 2014
  21. Deleted User

    Deleted User

    Guest

    Hello Guys,

    Does anyone know how to integrate Google+ or Facebook or Twitter or all the three using Unity instead of Eclipse? Thanks

    Regards,
    Bhanu
     
  22. hariavm

    hariavm

    Joined:
    Aug 18, 2014
    Posts:
    73
    use it
     

    Attached Files:

  23. Deleted User

    Deleted User

    Guest

    Hello Hariavm,

    Thanks for the FB pack but can you please explain how to use this package? When I simply run the scene FBCheck, A webpage opens with an error "This dialog has been passed a bad parameter." .Thanks a lot

    Regards,
    Bhanu
     
    Last edited by a moderator: Sep 15, 2014