- Home /
How to change cameras by a single touch on my phone's screen? (Android)
This is a code I made but when I test it on my phone it freezes and nothing happens (same in the editor mode when I'm connected with my phone via Remote4) So What I would like to is to change cameras by just a touch on the screen any time. How should I make it?
And what's the matter when I would like to change the Camera from mainCam to topCam only when the screen is touched, and when I release the screen with my finger it changes back to mainCam from topCam?
This code goes into Update()
while (Input.touchCount > 0) {
for (int i = 0; i < Input.touchCount; i++) {
if (Input.GetTouch (i).phase == TouchPhase.Ended && Input.GetTouch(i).tapCount == 1) {
if (mainCam.enabled){
mainCam.enabled = false;
topCam.enabled = true;
} else {
mainCam.enabled = true;
topCam.enabled = false;
}
}
}
}
Answer by Umresh · Jan 07, 2015 at 06:24 AM
Try using if instead of while.
if (Input.touchCount > 0) {
for (int i = 0; i < Input.touchCount; i++) {
if (Input.GetTouch (i).phase == TouchPhase.Ended && Input.GetTouch(i).tapCount == 1) {
if (prefab1.activeSelf){
prefab1.SetActive(false);
prefab2.SetActive(true);
} else {
prefab1.SetActive(true);
prefab2.SetActive(false);
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613175756im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
iOs/Android Development 2 Answers
Application.LoadLevel click problem 2 Answers
how i can convert thjs code to touch (Input.anyKeyDown) 2 Answers
Android on phone 1 Answer
Read / Write to phone's contact list. 0 Answers