- Home /
OnGui Function help
Hello Will. Doubt you will reply but on the off chance. I am using function OnGui and need when an object is click for it to change the image in a texture. much like the tutorial above but a bit more advanced.
var tex1 : Texture2D; var tex2 : Texture2D;
function OnGui{
if(GUILayout.Buttin((tex1), GUILayout.width(119),GUILayout.Height(119))){ Destroy(GameObject.FindWithTag(bike)); Instantiate(bigWheel2, Vector3(13,13,-507),Quaternion.identity); guiTexture.texture = tex2;
}
}
However i get the following error message: MissingComponentException: There is no GUITexture attatched to the ramp game object, but a script is trying to access it. You probably need to add the GUITexture to the game object ramp or your script needs to check if the component is attatched before using it.
It is definatly attatched to ramp so i need to check if its attatched before using it. How do i do this? i have tried if statements like:
if(tex1){ guiTexture.texture = tex2; }
but that doesnt work. any ideas?
Answer by Kourosh · Mar 22, 2011 at 02:51 PM
error handling:
var tex1:Texture2D;
function OnGUI(){
if(tex1 != null){ <<------It should work. if(GUI.Button(Rect(10,10,100,20), tex1) //blah blah } else { Debug.Log ("Please assign the texture."); }
}
i don't see how that could help :s
I need to swap the tex1 for tex2 when the gui button is clicked. so where it says
if(GUILayout.Button(tex1)),GUILayout.width(119),GUILayout.Height(119)))
i effectively need to write a script that changes that tex1 to tex2
I have fixed the problem... all i need is a variable called currentTex then tell that variable what texture to have at what time. eg.
if(GUILayout.Button((currentTex2), GUILayout.Width(119), GUILayout.Height(119))){ if(scorer >= 20){ Debug.Log("You have enough money"); Destroy(GameObject.FindWithTag ("bike")); gameObject.Find("Score").GetComponent(score).theScore-=10; Instantiate(bigWheel2, Vector3(13.58261, 13.05622, -507.1505),Quaternion.identity); bigWheel = GameObject.FindWithTag("bike").transform; bigWheel.transform.rotation = Quaternion
sorry what i meant was this:
if (GUILayout.Button((CurrentTex2), GUILayout.Width(119), GUILayout.Height(119))){
if(scorer>=20){ currentTex = tex2;
Your answer
Follow this Question
Related Questions
Render GUI elements to off screen camera 1 Answer
Web player screen corruption 1 Answer
Change GUI.Button Texture on runtime 1 Answer
Making texture cover whole button 1 Answer
GUI.DrawTexture on GUI.Button press 1 Answer