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
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
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;
}
}
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
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