0
\$\begingroup\$

I want to repeat a certain "Select (); - Deselect ();", as shown in the screenshot.

enter image description here

Prompt with advice how to implement it or what you need to read to implement this feature.

I tried to found something similar to what I want to do.

PlayerInteraction.cs from Farm Alarm game by Brackeys

private void OnTriggerEnter2D(Collider2D col)
    {
        if (target != col.gameObject && target != null)
        {
            Deselect();
        }

        target = col.gameObject;

        SeedBarrel barrel = target.GetComponent<SeedBarrel>();
        if (barrel != null)
        {
            barrel.Select();
        }

        SpriteRenderer[] srs = target.GetComponentsInChildren<SpriteRenderer>();
        foreach (SpriteRenderer sr in srs)
        {
            sr.color = new Color(1f, 1f, 1f, 0.9f);
        }
    }

    private void OnTriggerExit2D(Collider2D col)
    {
        if (col.gameObject == target)
        {
            Deselect();
            target = null;
        }
    }

    void Deselect()
    {
        SeedBarrel barrel = target.GetComponent<SeedBarrel>();
        if (barrel != null)
        {
            barrel.DeSelect();
        }

        SpriteRenderer[] srs = target.GetComponentsInChildren<SpriteRenderer>();
        foreach (SpriteRenderer sr in srs)
        {
            sr.color = Color.white;
        }
    }

}

But if I understand right it's way for 2D.

P.S. Screenshot from the game "Overcooked"

\$\endgroup\$
  • \$\begingroup\$ What have you tried so far? \$\endgroup\$ – DMGregory Jun 10 at 11:16
  • \$\begingroup\$ @DMGregory update ^^^? \$\endgroup\$ – Artik Slayer Jun 10 at 11:51
  • \$\begingroup\$ Great! How have you tried adapting this code for 3D? \$\endgroup\$ – DMGregory Jun 10 at 12:18

Your Answer

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

Browse other questions tagged or ask your own question.