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.