I am fairly new to unity but have had a run through the API and documentation and cannot see where i’m going wrong on this, and thought i would ask the community for an extra pair of eyes:
I have a simple class to demonstrate i have some basic grasp of a behaviour
//Should be placed on a GUIText object public class FrameCounter : MonoBehaviour { static Int64 Count; // Use this for initialization void Start() { this.useGUILayout = false; if (this.guiText == null) { Debug.LogError("warning guiText is null", this); } } // Update is called once per frame void Update() { FrameCounter.Count += 1; this.guiText.text = FrameCounter.Count.ToString(); Debug.Log(string.Concat(this.guiText.text, " frames since load."), this); } }
every frame this fires in the debug window and the game showing an ever increasing number. Not very functional but shows the frames are progressing and the code fires in OnUpdate
now i have tried to create a button that fires when the user presses is, using a GUITexture (avoiding the button class provided that uses onGUI)
//should be placed on a GUITexture object in the hierarchy public class PanelButton : MonoBehaviour { // Use this for initialization void Start() { this.useGUILayout = false; if (this.guiTexture == null) { Debug.LogError("warning the required guiTexture is missing", this); } } // Update is called once per frame void Update() { **Debug.Log("update called"); // only fires once when opening** //these only fire once if (Input.GetMouseButtonDown(0) && this.guiTexture.HitTest(Input.mousePosition)) { //we are inside the button and selecting it (in this frame) **Debug.Log("Update based Click detected");** } } }
The problem is the button only ever works once! the update only seems to fire once also. I am a little puzzled by this and would ask anyone who has a spare moment to take a look through the code and see if it jumps out.
running the framecounter on a guiText object and the Panel on a guiTexture reveal the following debug output:
> update called > > ... > > 198 frames since load > > 199 frames since load > > 200 frames since load > > ... > > Update based Click detected > > ... > > 230 frames since load > > 231 frames since load > > 232 frames since load >
which seems fine, but try pressing the button again… NOTHING happens, no “update called” messages whilst the N frames since load continues as normal.
From what i have read the “update called” should be firing as regularly as the framecounter, and the button down message for every time i press the button manually (as i cannot do that twice in one frame being human and all)
Any thoughts people? Thanks in advance, and even if you didn’t figure an answer thanks for taking the time to read it. I’ll be happy even if its a stupid mistake, and will update the issue if i ever fix it.
Pb