How to toggle between renderer and not to renderer with touch

Hi,
I have asked this question in answers section without much help there. I really need to understand how it works.

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);

                     }

                 }
             }
         }

     }
}

Try this:

        if (hit.collider.name == "B")
        {
            if (A_mesh.enabled == true)
            {
                A_mesh.enabled = false;
            }
            else
            {
                A_mesh.enabled = true;
            }
            Debug.Log("hit B!!!!!");
        }

In your code you allways tell the renderer to be enabled, but never to be disabled.
With this code you check if he is enabled before taking action. If he is: enabled = false, if not, = true.

Thanks Dimios, but unfortunately this does not work. my aim is to make A invisible when B is touched the second time. In short B is acting like an on/off button.

It is working for me, I replaced the touch part by mouse input to test. Do you have any error?
Do you have a camera set to “MainCamera”?

Also, you don’t need a getcomponent on start, since you click and drag the renderer directly in the inspector, you can just disable on start like this:

void Start ()
{
    A_mesh.enabled = false;
}

The Update is pretty much the same:

    void Update()
    {
        RaycastHit hit = new RaycastHit();
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.name == "B")
                {
                    if (A_mesh.enabled)
                    {
                        A_mesh.enabled = false;
                    }
                    else {
                        A_mesh.enabled = true;
                    }
                    Debug.Log("hit B!!!!!");
                }
            }
        }
    }