OnMouseDown() & OnMouseUp()

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

Could you post your code?

When you select something, save the object selected. When you select another one, deselect the old one. That’s the theory… :slight_smile:

2 Likes

code???

Exactly what @Fraconte said, if you don’t know how to so that, then please share your code with us.

code = program, script

I think he was asking for the code…

Well, this has been a good thread everybody. Champagne all around!

Ops! Sorry… I was reading StarManta asking for code and thought “Code???” was the answer of Bhanu3d. :slight_smile:

i am askng for ur code used in the script man

You could have something in the update() function like so:

if(firstBool == true)
{
    secondBool = false;
}
else
{

  firstBool = true;
  secondBool = false;

}

I have no idea if this would work. I am only learning C# through trial and error… In theory it should work…

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

Try this:

using UnityEngine;
using System.Collections;

public class SelectOne : MonoBehaviour
{
    static public SelectOne Current;
    public bool Selected = false;

    void OnMouseDown()
    {
        if (Current != null)
            Current.Selected = false;
        Selected = true;
        Current = this;
    }
}
1 Like

i thought Fraconte s right

Hello Guys,

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

Regards,
Bhanu

1 Like

cool.always welcome

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

Use: Random.range (1, 5) * 5

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

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