1
\$\begingroup\$

Please see the gif below to better understand what's going on.

enter image description here

On first click they all work normally but after first click I have to double click them to select them. I'm not a advanced developer so I've been trying to figure this out for straight 2 hours and last choice I wanted to ask you guys.

I declared select effect here. (White circle) And added an Array to get all select effects.

    public GameObject selectEffect;
    private bool isSelected = false;
    public GameObject[] selectEffectArray;

I put them inside the Array.

    private void Awake()
    {
        selectEffectArray = GameObject.FindGameObjectsWithTag("planetSelected");
        selectEffect = transform.Find("selectEffect").gameObject;
    }

I deactivate all select effects on start.

    void Start()
    {
        Deactivate();
    }

Here's Deactivate code.

    void Deactivate()
    {
        foreach (GameObject effect in selectEffectArray)
        {
            effect.SetActive(false);
        }
    }

When I click one of the planets these happens. (I'm a little confused so I hope you can understand it.)

    void OnMouseUp()
    {
        if (isSelected)
        {
            selectEffect.SetActive(false);
            travelbutton.SetActive(false);
            isSelected = false;
        }
        else
        {
            Deactivate();

            planetName_text.text = planet.name;
            requiredFuel_text.text = planet.requiredFuel.ToString();
            travelbutton.SetActive(true);
            selectEffect.SetActive(true);
            isSelected = true;
        }

    }

I don't want to double click a planet to select it.

\$\endgroup\$
0
\$\begingroup\$

What happens here is basically this:

When you click on a planet for the first time, isSelected is set to true. Then you click away, but since you don't check whether other planets were selected, isSelected remains true. The next time you try to select it, it first detects, that it is indeed the current active planet (based on the variable) and deactivates itself.

Instead of letting every planet handle this themselves, you need to make sure they consult with a parent object. Let's call it SelectionManager.

Every time a planet detects a click, send a message to the selection manager. This should set the selected planet to the one that sent the message:

class SelectionManager 
{

    private GameObject selected = null; // null means no gameobject is currently the chosen one

    public void HandleSelection(GameObject planet) 
    {
        if (selected == planet) // The user clicked on the selected planet again
        {
            selected = null;  // Deselect the planet
        } 
        else 
        {
            selected = planet;
        }
    }

}

And inside the planet gameobjects:

public SelectionManager selectionManager; // Set this using the drag 'n' drop feature

//....

void OnMouseUp()
{
    selectionManager.HandleSelection(this);
}

You can get the currently selected planet from the selection manager.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.