0
\$\begingroup\$

enter image description here

So I have two gameObjects, the blue one the player character and different spheres of difference colors. Each sphere has a function lets say SphereFunction().

When the player character touches a sphere gameobject I want a button to appear on the screen. This can be done using the ontriggerenter2d and ontriggerexit2d.

enter image description here

What I want is the button OnClick to call the SphereFunction() of the sphere if pressed. This function can have different implementations depending on the color of the sphere, just the function name is the same.

How can this be done using a single button?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You can do something like this:

//Attach this script to a canvas prefab containing your button, and link the
//button reference in the Inspector
public class ButtonDialog : MonoBehaviour {
    [SerializeField] private Button button;
    
    public Button Button => button;

    //you can also set up a close button that calls this function
    public void Close() {
        Destroy(gameObject);
    }
}

//Base class for the sphere component. Displays the dialog and listens for the button 
//click; actual click handling will be defined in child classes
abstract public class Sphere : MonoBehaviour {
    [SerializeField] private ButtonDialog dialogPrefab;

    //display the dialog and add the click event listener
    void OnTriggerEnter(Collider collider) {
        ButtonDialog instance = Instantiate(dialogPrefab);
        instance.Button.onClick.AddListener(OnClickButton);
    }

    abstract protected void OnClickButton();
}

//You can create child classes that extend Sphere and define the click functionality. 
//I have no idea what functionality you want, so here's a random example.
public class GrowingSphere : Sphere {
    override protected void OnClickButton() {
        //double the size of the sphere
        Vector3 scale = transform.localScale;
        scale *= 2;
        transform.localScale = scale;
    }
}
  1. Create a canvas with the button. Add the ButtonDialog component to the canvas, and drag the canvas into the Project panel to create a prefab.

  2. Create a sphere and add the GrowingSphere component (or whatever component you create that extends Sphere). In the Inspector for the sphere, link the dialogPrefab field to the prefab you created in step 1.

You can create as many components as you like that extend Sphere and implement their own version of OnClickButton()

\$\endgroup\$
2
  • \$\begingroup\$ Sorry for not responding earlier. I've been looking over your code and tried to implement something similar to that. Having a base abstract class with a listener for onClick button and inheriting that class in another and implementing the abstract method simply reminded me of my college days and all the OOP principles I should have paid more attention to :(. But anyways, thanks alot for the help! Solved my problem! \$\endgroup\$
    – Stefan
    Dec 28, 2020 at 22:30
  • \$\begingroup\$ @Stefan Great, glad to help. OOP is very important, so you should brush up on it if you are rusty. Also, please review my answer to your previous question if you haven't already: gamedev.stackexchange.com/questions/187897/… \$\endgroup\$
    – Kevin
    Dec 28, 2020 at 23:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .