How can I change the texture of a game object by clicking on another game object?


I am trying to make a menu that is setup like a journal where you click on a cube and it changes the texture of a plane to the next page. The only problem is I have no clue how to do it.

I just started working with Unity so thanks for your help.

Here is a bit of code to get you started. You attach it to the cube, then in the inspector you need to drag and drop the game object that will be used to display the pages onto the ‘pagesGameObject’ variable. You will also have to set the size and drag the textures for each page onto the ‘pages’ variable.

Note if you are going to create more controls for your pages beyond just next page, you will need to split out the page handling script into its own script attached to the pages game object. Then you will need to learn how to communicate between game objects. One way is described here:

#pragma strict

var pagesGameObject : GameObject;
var pages : Texture[];
private var currPage = 0;

function Start() {
	pagesGameObject.renderer.material.mainTexture = pages[currPage];

function OnMouseDown() {
	currPage = (currPage + 1) % pages.Length;
	pagesGameObject.renderer.material.mainTexture = pages[currPage];