Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Button Pushing Conditions-Help Please?

Discussion in 'Scripting' started by KDamon, Oct 3, 2017.

  1. KDamon

    KDamon

    Joined:
    Sep 21, 2017
    Posts:
    33
    I'm using unity. I'm trying to make it so when I click one of three buttons, it does something unique from the other buttons. How do I set up this condition in a function? I want to say when this button is clicked, it does this, and when this button is clicked it does this. Do I have to declare a public game object for that button and then attach it to the button in unity? How do I connect the buttons in unity with the buttons declared in the script?

    Here's what I'm trying to do
    Code (CSharp):
    1.  
    2. public class factory : MonoBehaviour
    3. {
    4.     public GameObject sphereButton;
    5.     public GameObject cubeButton;
    6.     public GameObject cylinderButton;
    7.  
    8.     public interface Shape
    9.     {
    10.         GameObject CreateShape();
    11.     }
    12.  
    13.  
    14.  
    15.     public class Sphere : Shape
    16.     {
    17.  
    18.         public GameObject CreateShape()
    19.         {
    20.             GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    21.             sphere.transform.position = new Vector3(0, 0, 0);
    22.             sphere.GetComponent<MeshRenderer>().material.color = new Color
    23.                    (Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1));
    24.             Destroy(sphere, 30.0f);
    25.             return sphere;
    26.         }
    27.     }
    28.  
    29.  
    30.  
    31.     public class Cube : Shape
    32.     {
    33.         public GameObject CreateShape()
    34.         {
    35.             GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    36.             cube.transform.position = new Vector3(0, 0, 0);
    37.             cube.GetComponent<MeshRenderer>().material.color = new Color
    38.                    (Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1));
    39.             Destroy(cube, 30.0f);
    40.             return cube;
    41.         }
    42.     }
    43.  
    44.  
    45.  
    46.     public class Cylinder : Shape
    47.     {
    48.         public GameObject CreateShape()
    49.         {
    50.             GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    51.             cylinder.transform.position = new Vector3(0, 0, 0);
    52.             cylinder.GetComponent<MeshRenderer>().material.color = new Color
    53.                    (Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1));
    54.             Destroy(cylinder, 30.0f);
    55.             return cylinder;
    56.         }
    57.     }
    58.  
    59.  
    60.  
    61.     public class ShapeFactory
    62.     {
    63.         public Shape GetShape(Button shapeButton)
    64.         {
    65.             if (shapeButton.tag == "Cube Button")
    66.             {
    67.                 return new Cube();
    68.             }
    69.             if (shapeButton.tag == "Sphere Button")
    70.             {
    71.                 return new Sphere();
    72.             }
    73.             if (shapeButton.tag == "Cylinder Button")
    74.             {
    75.                 return new Cylinder();
    76.             }
    77.             return null;
    78.         }
    79.     }
    80. }
     
    Last edited: Oct 4, 2017
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    This just turned up on search, I'm sure there are many. Your question is really too general for this forum. It needs a tutorial.
     
  3. KDamon

    KDamon

    Joined:
    Sep 21, 2017
    Posts:
    33
    I just added some code
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819