Renderer.enabled script: Objects are flashing visible sometimes when they shouldn't

Hello forum,

I have a Unity application that is using the Vuforia augmented reality SDK. I’m using scripts to control the visibility of some game objects.

I have a “Next” button that will increase the count of a counter. The scripts on my objects look at this counter and will set Renderer.enabled to True when the count is high enough.

I have 15 objects. Each object has it’s own script with a different counter threshold.

The code seems to work pretty well. But occasionally all of my hidden objects will appear very briefly. So far, I have not been able to figure out why.

Next Button Code:

using UnityEngine;
using System.Collections;

public class NextButton2 : MonoBehaviour
{
public int nextcounter=0; //Init counter at zero

    void OnGUI ()
    {
            // Make a background box
        GUI.Box(new Rect(0,0,0.1f*Screen.width,0.16f*Screen.height), "Press Next");
       
        // Make the first button. If it is pressed, the variable will be increased
        if(GUI.Button(new Rect(0.005f*Screen.width,0.04f*Screen.height,0.085f*Screen.width,0.04f*Screen.height), "Next")) {
            // This code executed when Button is clicked
            nextcounter ++;
            Debug.Log ("counter = " + nextcounter);
        }
        // Make the second button. If it is pressed, the variable will be reset
        if(GUI.Button(new Rect(0.005f*Screen.width,0.1f*Screen.height,0.085f*Screen.width,0.04f*Screen.height), "Reset")) {
            // This code executed when Button is clicked
            nextcounter=0;
            Debug.Log ("counter = " + nextcounter);
    }
}
}

Activate Object Code:

using UnityEngine;
using System.Collections;

public class Activate01 : MonoBehaviour {
    public GameObject player;
    private NextButton2 playerScript;

    void Start () {
                playerScript = player.GetComponent<NextButton2> ();
        }
    // Update is called once per frame
    void Update () {
        if(playerScript.nextcounter >= 1) {
            Debug.Log ("01 On");
            renderer.enabled = true;
    }
        if(playerScript.nextcounter < 1) {
            Debug.Log ("01 Off");
            renderer.enabled = false;
}
}
}

I’d love some insight on perhaps a better, more reliable way to hide my objects based on counter count. Or, any ideas on what might be going wrong and how I could troubleshoot it.

Are the flickering objects children of a target? One of the irritating things about the default handlers in Vuforia is that they muck around with the renderers of all their children every time the detection/tracking state changes. For example, if you go into extended tracking or return from extended tracking to regular tracking, it is gonna make all your renderers visible.

If that’s the issue, you could fix it by changing/replacing the trackable event handler script on your target. You might also try moving your own code to LateUpdate instead - that way even if Vuforia changes your renderers’ Enabled properties, your own script will have a chance to override it before it’s rendered.

1 Like

Hello StarManta,

Thank you for your reply.

The flickering objects are indeed children of a target. I had a feeling that Vuforia might be the culprit here, and my application is very likely moving in and out of extended tracking.

I’m still very new to both Unity and Vuforia, so it’s going to be a challenge to replace those scripts. But it’s a big help to know where to start.

-edit-

I’m not sure if I’ve done this correctly or not, but it seems to be working.

All I did was change my Activate scripts to use LateUpdate instead of Update.

I didn’t think this would work, as I figured the DefaultTrackableEventHandler would now for sure run faster than my activate code and the flickering would be worse. As I understand, things in LateUpdate happen only after all code called in Update.

In any case, it does seem to be working. Although I think the camera images might be a little choppier now.