Object is set to be active but reminds unactive

I'm trying to crack one problem. I am doing a VR game. I set one invisible collider to be a trigger for switching from hand avatar that is on controllers to something else in the game. The issue is that the trigger is being recognized but setting it active just doesn't work. I'll appreciate any tips

I tried to switch the object both in the update method and in on trigger methods. Previously in the update method, it didn't pass the data from the on the trigger. But if I have it in the trigger, the bool is recognized but wouldn`t set the object active.

public GameObject leftHand;
    public GameObject leftHandOnChest;
    public bool cprColliderLeft = false;

    void Start()
    {
        leftHandOnChest.SetActive(false);
        leftHand.SetActive(true);
    }
    void Update()
    {

    }

    private void OnTriggerEnter(Collider collider)
    {
        if (collider.tag == "CPRStart")
        {

            cprColliderLeft = true;

            if(cprColliderLeft == true)
            {
                leftHandOnChest.SetActive(true);
                leftHand.SetActive(false);
            }
        }
    }

    private void OnTriggerStay(Collider collider)
    {
        if (collider.tag == "CPRStart")
        {
            cprColliderLeft = true;

            if (cprColliderLeft == true)
            {
                leftHandOnChest.SetActive(true);
                leftHand.SetActive(false);
            }

        }
    }

    private void OnTriggerExit(Collider collider)
    {
        cprColliderLeft = false;
        if(cprColliderLeft == false)
        {
            leftHandOnChest.SetActive(false);
            leftHand.SetActive(true);
        }
    }

My goal is to switch one object from being active to the other one. So one would disappear from the scene and the other one would show up. Thanks for any help

Upvotes: 1

Views: 90

Answers (1)

KYL3R
KYL3R

Reputation: 4073

I would suggest this simplified code:

public GameObject leftHand;
public GameObject leftHandOnChest;

void Start()
{
    leftHandOnChest.SetActive(false);
    leftHand.SetActive(true);
}

private void OnTriggerEnter(Collider collider)
{
    if (collider.compareTag ("CPRStart"))
    {
        leftHandOnChest.SetActive(true);
        leftHand.SetActive(false); 
    }
}

private void OnTriggerExit(Collider collider)
{
    if (collider.compareTag ("CPRStart"))
    {
        leftHandOnChest.SetActive(false);
        leftHand.SetActive(true);
    }
}

But I guess your problem may be some enter/exit chain-reaction. I guess the tag "CPRStart" is on the leftHand - so as you enter, you deactivate it - which results in exit getting called.

Therefore I would suggest to disable the renderer only:

public GameObject leftHand;
public GameObject leftHandOnChest;
private MeshRenderer hand_renderer;

void Start()
{
    leftHandOnChest.SetActive(false);
    leftHand.SetActive(true);
    hand_renderer = leftHand.getComponent <MeshRenderer> ();
}

private void OnTriggerEnter(Collider collider)
{
    if (collider.compareTag ("CPRStart"))
    {
        leftHandOnChest.SetActive(true);
        hand_renderer.enabled = false;
    }
}

private void OnTriggerExit(Collider collider)
{
    if (collider.compareTag ("CPRStart"))
    {
        leftHandOnChest.SetActive(false);
        hand_renderer.enabled = true;
    }
}

Upvotes: 1

Related Questions