Touch once to activate and touch again to deactivate gameObject using Renderer
Hi all I am trying to learn my way through Unity and have setup a scene
I have A as prefab (custom) and B (sphere) of which A has 2 sphere gameobject as child. When the scene starts A is hidden via renderer.
My aim is when B is touched A should get activated and when B is touched again A should get deactivated. I have manage to achieve the first part ( Touch B to activate A), but cannot get A to deactivate again.
Please help, the script follows
using UnityEngine;
using System.Collections;
public class touchtest1 : MonoBehaviour {
public Renderer A_mesh;
public GameObject A_prefab;
void Start() {
A_mesh.GetComponent<Renderer>().enabled = false;
}
void Update () {
RaycastHit hit = new RaycastHit();
for (int i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch (i).phase == TouchPhase.Began) {
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (i).position);
if (Physics.Raycast (ray, out hit)) {
if (hit.collider.name == "B") {
Debug.Log ("hit B!!!!!");
A_mesh.enabled = true;
A_prefab.SetActive (true);
}
}
}
}
}
}
bool isAObjActive = false;
if(hit == "B" && !isAObjActive)
isAObjActive = true;
else if( hit == "B" && isAObjActive)
isAObjActive = false;
Hi Ahmed,
I tried your code suggestion doesn't seems to work atleast in my code.
Please elaborate with code.
thanks
Your answer
Follow this Question
Related Questions
2D touch - Why is this movement so jittery? 1 Answer
How do I make a rigid body move towards position of touch 0 Answers
iOS moving up and down question ( code problem ) 0 Answers
Enhanced touch 0 Answers
How to separate a Tap from a Swipe? 0 Answers