- Home /
Call GUITexture and script
Help! I have made a GUITexture and a script that will change the texture and I want to call both the GUItexture and its script from another script. Is it possible or is there a way to call both of them or can I call it like this?
example:
quizzes.gameObject.guiTexture.script?
or something like that.
Answer by phfatmonkey · Feb 08, 2013 at 12:58 AM
Not completely clear what you're asking, but I've found the easiest way to call a script from another script is to assign that object to a public variable, and then use GetComponent to find the script attached to it. Here's an example:
//this script is attached to your GUITexture
using UnityEngine;
using System.Collections;
public class GUITextureScript : MonoBehaviour {
public void ChangeTexture ()
{
//change texture
}
}
//this class would be attached to the object from which you want to call/use the GUITexture
using UnityEngine;
using System.Collections;
public class SomeOtherObjectScript: MonoBehaviour {
public GUITexture guiTx;
//then use GetComponent to get the script attached
private void AMethodThatCallsScriptAttachedToGUITexture ()
{
GUITextureScript tex_script = guiTx.GetComponent<GUITextureScript>();
tex_script.ChangeTexture ();
}
}
In the Editor just drag the GUITexture object onto the property slot in the script that is attached to the object from which you wish to call the GUITexture script.
Make sense?