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.