0

How can I create a touch event or UI button that can execute the code below. I got that code from an asset that meant for PC i want to re-create that project for android mobile

using UnityEngine;

using System.Collections;

public class PlayerIO : MonoBehaviour {

RaycastHit hit;
int maxBuildDist = 10;
public Transform RetAdd;
public Transform RetDelete;
public Transform Player;
public GameObject Player_Camera;
Color Block_Color = Color.blue;
bool Block_Menu = false;

void Start(){
    RetAdd = GameObject.Find("RetAdd").transform;
    RetDelete = GameObject.Find("RetDel").transform;
    Player = GameObject.FindGameObjectWithTag("Player").transform;
    Player_Camera = GameObject.Find("Main Camera");
}

void Update(){
    if(Input.GetKeyDown(KeyCode.E))
        Block_Menu = !Block_Menu;
    if(Block_Menu){
        Time.timeScale = 0;
        Player.GetComponent<MouseLook>().enabled = false;
        Player_Camera.GetComponent<MouseLook>().enabled = false;
        //((MonoBehaviour)Player.GetComponent<FPSInputController>()).enabled = false;
    }
    if(!Block_Menu){
        Time.timeScale = 1;
        Player.GetComponent<MouseLook>().enabled = true;
        Player_Camera.GetComponent<MouseLook>().enabled = true;
        //((MonoBehaviour)Player.GetComponent<FPSInputController>()).enabled = true;
    }

    if(!Block_Menu){
        if(Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3((Screen.width / 2), (Screen.height / 2), 0)), out hit, maxBuildDist)){
            RetAdd.GetComponent<Renderer>().enabled = true;
            if(hit.transform.tag == "Block"){
                RetAdd.transform.position = hit.transform.position + hit.normal;
                RetDelete.transform.position = hit.transform.position;
                RetDelete.GetComponent<Renderer>().enabled = true;
            }
            if(hit.transform.tag != "Block"){
                RetDelete.GetComponent<Renderer>().enabled = false;
                RetAdd.transform.position = new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z);
            }
            if(Input.GetMouseButtonDown(0)){
                GameObject block = (GameObject)Instantiate(Resources.Load("Block01"), RetAdd.transform.position, Quaternion.identity);
                block.GetComponent<Renderer>().material.color = Block_Color;
            }
            else if(Input.GetMouseButtonDown(1) && hit.transform.tag != "Floor"){
                Destroy(hit.transform.gameObject);
            }
        }
        else{
            RetAdd.GetComponent<Renderer>().enabled = false;
            RetDelete.GetComponent<Renderer>().enabled = false;
        }
    }
}

void OnGUI(){
    if(Block_Menu)
        Block_Menu_GUI();
}

void Block_Menu_GUI(){
    GUILayout.BeginVertical();
    if(GUILayout.Button("Green"))
        Block_Color = Color.green;
    if(GUILayout.Button("Red"))
        Block_Color = Color.red;
    if(GUILayout.Button("Blue"))
        Block_Color = Color.blue;
    GUILayout.EndVertical();
}

}

this is the whole code of the script that I want to change. I already had a joystick control but the problem is since I'm using that OnMouseDown whenever I click my joystick the object is spawning

Ashton
  • 1
  • 2
  • You want to create a UI button from code? Please format your code. – Programmer Sep 23 '17 at 13:08
  • Maybe he want to create an OnClick-Event, but I don't know – DogeAmazed Sep 23 '17 at 13:19
  • I've watched some tutorial on youtube on how to create a minecraft like game the problem is the script that I've copy from the video is for Pc and Mac every time I use the joystick for movement that GetMouseButtonDown is initializing. I want to put that event to UI button. – Ashton Sep 23 '17 at 13:29
  • Create a button then use `button.onClick.AddListener` from the duplicate....Please don't say it doesn't work. If it's not working then update your question with your new code – Programmer Sep 23 '17 at 13:58
  • already post the whole code of the script – Ashton Sep 23 '17 at 14:42
  • @Programmer I'm not familiar with button.onClick.Addlistener sorry to say but I have to admit that I'm just a beginner and I just follow the tutorials from youtube :( – Ashton Sep 23 '17 at 14:45
  • Check #2 from the duplicate. It has an example – Programmer Sep 23 '17 at 14:46
  • @Programmer Hello sir thank you so much for helping It works well :D sorry if I asked inscrutable question. thanks a lot again :D – Ashton Sep 23 '17 at 15:08
  • No problem and you are welcome! – Programmer Sep 23 '17 at 15:11

0 Answers0