Change background pictures when picture clicked

Hi,

I am developing a children’s game App u sing Javascript. I have 3 different pictures(as GUITextures) which i want the user to click one of them and it changes the background colour. if user clicked the second one it must remove the pervious one and apply the current one. I don’t know how to go ahead with this as i have added 3 background pictures and all are in one place at the same time…Please help

Am very new to Unity Javascript and this is not an excuse on not knowing,but wanna learn

Thanks for your help,

I know you specified JavaScript, but I much prefer C# (and it is easy enough to convert using online tools… and I can’t do all the work for ou, now can I?). With very limited knowledge as to what you are trying to accomplish, here is what I would do.

If you are using all GUI for the interface, you could do this with a very simple script…

using UnityEngine;
using System.Collections;

public class BackgroundTextureController : MonoBehaviour 
{
	// an array of textures
	public Texture[] myTexture;

	// the specific texture within the array to display
	public int textureNumber;

	int margin = 10;

	void Start()
	{
		textureNumber = 0;
	}

	void OnGUI()
	{
		// creates area for teture to be displayed
		GUILayout.BeginArea( new Rect (margin, 
		                               margin, 
		                               Screen.width / 4, 
		                               Screen.width / 4));

		// sets the texture of a box in the area to equal a specific 
		// position in an array of textures
		GUILayout.Box(myTexture[textureNumber]);

		// ends the area
		GUILayout.EndArea();


// if you are using a GUI background, but gameObjects to alter the background, 
// remove following code until "Stop removing code"

		// creates area to hold buttons for changing position in array
		GUILayout.BeginArea( new Rect (margin, 
		                               Screen.width / 4 + margin, 
		                               Screen.width / 4 + margin, 
		                               Screen.width / 4 + margin));

		// each button changes the texture being displayed from
		// your array of textures
		if(GUILayout.Button("Texture1"))
			textureNumber = 0;

		if(GUILayout.Button("Texture2"))
			textureNumber = 1;

		if(GUILayout.Button("Texture3"))
			textureNumber = 2;

		GUILayout.EndArea();

// Stop removing code

	}
}

However, if you want to use a combination of GUI and gameObjects, you would need a second script to attach to those game objects that would alter the texture displayed from the array.

using UnityEngine;
using System.Collections;

public class TextureChanger : MonoBehaviour 
{
	// the texture from the array on bTC this object will change background to 
	public int textureToDisplay;

	// drop gameObject that contains the "BackGroundTextureController" script here
	public GameObject backgroundController;

	BackgroundTextureController bTC;


	void OnMouseDown()
	{
		bTC = (BackgroundTextureController)backgroundController.GetComponent("BackgroundTextureController");

		bTC.textureNumber = textureToDisplay;
	}
}

However, a simpler way of approaching the GUI and gameObject solution would be to use two scripts that do the following…

Script 1 (Controls the GUI background)

using UnityEngine;
using System.Collections;

public class BackgroundController : MonoBehaviour 
{
	public Texture backgroundTexture;

	int margin = 10;

	void OnGUI()
	{
		GUILayout.BeginArea( new Rect (margin, margin, Screen.width / 4, Screen.width / 4));
		GUILayout.Box(backgroundTexture);
		GUILayout.EndArea();
	}
	
}

Script 2 (Attached to gameObjects that will change the background)

using UnityEngine;
using System.Collections;

public class BackgroundChanger : MonoBehaviour {

	// place object here that has "BackgroundController" script attached
	public GameObject textureController;

	// place texture here that you want the background to change to
	// when the gameObject is clicked
	public Texture thisTexture;

	BackgroundController bControl;

	void OnMouseDown()
	{
		bControl = (BackgroundController)textureController.GetComponent("BackgroundController");
		bControl.backgroundTexture = thisTexture;
	}
}

Or, if you wanted the background to be a gameObject (rather than a GUI), use the following scripts…

Script 1 (on object whose texture you want to change)

using UnityEngine;
using System.Collections;

public class BackgroundController : MonoBehaviour 
{
	public Texture backgroundTexture;

	int margin = 10;

	void Start()
	{
		ChangeBackgroundTexture();
	}

	public void ChangeBackgroundTexture()
	{
		// rather than setting the texture in Update, we can control
		// when the texture is changed.

		renderer.material.mainTexture = backgroundTexture;	
	}
}

Script 2 (on objects to press, that will change the texture of background)

using UnityEngine;
using System.Collections;

public class BackgroundChanger : MonoBehaviour {

	// place object here that has "BackgroundController" script attached
	public GameObject textureController;

	// place texture here that you want the background to change to
	// when the gameObject is clicked
	public Texture thisTexture;

	// accessthe background controller script
	BackgroundController bControl;

	void OnMouseDown()
	{
		bControl = (BackgroundController)textureController.GetComponent("BackgroundController");

		// sets the new background texture
		bControl.backgroundTexture = thisTexture;

		// applies the new background textures
		bControl.ChangeBackgroundTexture();
	}
}

Hope that helps.

A.

Color color = new Color[3];
string str = {“White”, “Blue”, “Black”};

void Start(){
   color[0] = Color.white;
   color[1] = Color.blue;
   color[2] = Color.black;
}
void OnGUI(){
   for(int i = 0; i < 3;i++){
      if(GUI.Button(new Rect(0,100 * i,100, 100), str*)){*

Camera.main.backgroundColor= color*;*
}
}
}
This still needs you to change the colors for pictures and remove the texts, but the logic is there.