Search Unity

Pushing a GameObject Button on Mobile (Help)

Discussion in 'Scripting' started by bonjelaboy, May 6, 2020.

  1. bonjelaboy

    bonjelaboy

    Joined:
    May 6, 2020
    Posts:
    2
    Hi all, trying to make an AR app where I click on a button represented by a gameobject and then the button changes the visibility of a separate gameobject. It should cast a raycast from where was touched to the object and then if it hits a button; cycle it between three states which change the buttons colour and the object associated with its visibility.

    I feel like I'm close but when I press the button nothing happens, no sound, no colour change no animation.

    Can anyone take a look at my code and give me a pointer? Think I might be trying to change the color incorrectly.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class ButtonPress : MonoBehaviour
    7. {
    8.     //State
    9.     public int ButtonState;
    10.  
    11.     //Sounds
    12.     public AudioClip ButtonClickNoise;
    13.  
    14.     //Animations
    15.     public Animation ButtonPressAnimation;
    16.  
    17.     //GameObjects and Materials
    18.     public GameObject ButtonCollider;
    19.     public GameObject MACEPart;
    20.     public GameObject ButtonPart;
    21.     public Material PartMaterial;
    22.     public Material ButtonMaterial;
    23.  
    24.     //Raycast Stuff
    25.     public GameObject RaycastReturn;
    26.  
    27.     //Line Renderer
    28.     private LineRenderer LineLink;
    29.  
    30.     void Start()
    31.     {
    32.         Debug.Log("Button Start Was Called.");
    33.  
    34.         ButtonState = 0;
    35.         PartMaterial = MACEPart.GetComponent<MeshRenderer>().material;
    36.         ButtonPart.GetComponent<MeshRenderer>().material.color = Color.green;
    37.         LineLink = ButtonPart.GetComponent<LineRenderer>();
    38.         ButtonPressAnimation = ButtonPart.GetComponent<Animation>();
    39.  
    40.         //Draw line between objects
    41.         LineLink.SetPosition(0, ButtonPart.transform.position);
    42.         LineLink.SetPosition(1, MACEPart.transform.position);
    43.         LineLink.enabled = true;
    44.     }
    45.  
    46.     // Update is called once per frame
    47.     void Update()
    48.     {
    49.         if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
    50.         {
    51.             //Raycast Hit Detection
    52.             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    53.             RaycastHit hit;
    54.             if (Physics.Raycast(ray, out hit))
    55.             {
    56.                 if (hit.collider != null)
    57.                 {
    58.                     if (ButtonCollider = hit.collider.gameObject) //if the raycast has hit the button
    59.                     {
    60.                         Debug.Log(this.gameObject.name + " Was Clicked."); //to check if actually working. remove if it is
    61.  
    62.                         ButtonPressAnimation.Play();
    63.                         AudioSource.PlayClipAtPoint(ButtonClickNoise, gameObject.transform.localPosition);
    64.  
    65.                         switch (ButtonState) //Do something based on the Buttons State
    66.                         {
    67.                             case 0: //Visible component
    68.  
    69.                                 MACEPart.SetActive(true);
    70.                                 Color Visible = PartMaterial.color;
    71.                                 Visible.a = 1.0f;
    72.                                 PartMaterial.SetColor("Visible", Visible);
    73.  
    74.                                 ButtonMaterial.SetColor("Green", Color.green);
    75.  
    76.                                 ButtonState = 1;
    77.                                 break;
    78.  
    79.                             case 1: //Semi-Transparent
    80.                                 MACEPart.SetActive(true);
    81.                                 Color SemiTransparent = PartMaterial.color;
    82.                                 SemiTransparent.a = 0.3f;
    83.                                 PartMaterial.SetColor("Semi-Transparent", SemiTransparent);
    84.  
    85.                                 ButtonMaterial.SetColor("Amber", Color.yellow);
    86.  
    87.                                 ButtonState = 2;
    88.                                 break;
    89.  
    90.                             case 2: //Semi-Transparent
    91.                                 MACEPart.SetActive(false);
    92.  
    93.                                 ButtonMaterial.SetColor("Red", Color.red);
    94.  
    95.                                 ButtonState = 0;
    96.                                 break;
    97.  
    98.                             default:
    99.                                 Debug.Log("Button is not in one of the three states!");
    100.  
    101.                                 break;
    102.                         }
    103.                     }
    104.                 }
    105.             }
    106.         }
    107.     }
    108. }
     
  2. bonjelaboy

    bonjelaboy

    Joined:
    May 6, 2020
    Posts:
    2
    Anyone able to help with this?