Change texture of a GUITexture

Hi,

I search how to change texture of a GUITexture. I found many replies on the forum but all are outdated or just not working :confused: .

It’s a simple button on/off for playing music.

My code at the moment :

public class ToggleSound : MonoBehaviour {

	int sound = 1;
	public GUITexture Music_on;
	public GUITexture Music_off;

	void savePref(int sound){
		PlayerPrefs.SetInt("music", sound);
	}

	void getPref(){
		PlayerPrefs.GetInt("music");
	}

	void OnMouseDown(){
		if (sound == 1){
			GameObject.Find("Space").audio.Stop();
			sound = 0;
			savePref(sound);
			GameObject.Find("musicSwitch").guiTexture.texture = Music_off; // error here
		} else {
			GameObject.Find("Space").audio.Play();
			sound = 1;
			savePref(sound);
			GameObject.Find("musicSwitch").guiTexture.texture = Music_on; // error here
		}
	}
}

The error :

Assets/Menu/ToggleSound.cs(23,67): error CS0029: Cannot implicitly convert type UnityEngine.GUITexture' to UnityEngine.Texture’

L 23 : GameObject.Find(“musicSwitch”).guiTexture.texture = Music_off;

Thanks

im not sure the GameObject.Find is needed, unless this is going on to an object that isn’t music switch. Just try

guiTexture.texture = whatever;

that should refer to the guiTexture component upon the game object calling the script.

Also you could try just using Texture2D for your variable instead of GUITexture because a GUITexture is simply a component that accepts a Texture2D. Untested, good luck.