I have a problem with my coding. To me the codes below is correct and make sense .
Unfortunately it does not work very well. The script does not change as what I expected.
My expectation was
When a button is clicked.
The button will execute an external script which will change the texture of another 3D Object.
using UnityEngine;
using System.Collections;
public class Calling : MonoBehaviour {
int i = 0;
//Create an Object reference named switchImage which is refereing to the SwitchImage Class.
SwitchImage switchImage=new SwitchImage();
// Update is called once per frame
public void OnMouseDown () {
switchImage.Update();
}
}
using UnityEngine;
using System.Collections;
using System;
public class SwitchImage: MonoBehaviour{
public Texture diffuseTexture0;//Textures 0
public Texture diffuseTexture1;//Textures 1
public Texture diffuseTexture2;//Textures 2
//Texture Size
static int textureSize = 3;
public int i = 0;
private bool start = false;
public void Update()
{
Texture[] textureArray = new Texture[textureSize];
textureArray[0] = diffuseTexture0;
textureArray[1] = diffuseTexture1;
textureArray[2] = diffuseTexture2;
//set the texture according to the array
renderer.material.SetTexture("_MainTex", textureArray[i]);
//Increment the i Variable.
i++;
//Check whether the value of i is equal or bigger than the value of textureArray.
//If yes, then set i = 0
if(i>=textureSize){
i=0;
}
}
}
It’s a problem you call your function “Update” as that’s a built-in function name, used to do stuff each frame.
Try calling it “DoTextureSwitch” or something like that. Right now I would guess your image just switches once per frame;)
Btw, your code is a little messy, and not very optimized. Here is some ideas for improvements…
using UnityEngine;
using System.Collections;
using System;
public class SwitchImage: MonoBehaviour{
public Texture2D[] diffuseTextures;
public int indexPointer = 0;
//What does this do...?
private bool start = false;
public void DoTextureSwitch()
{
//set the texture according to the array
renderer.material.SetTexture("_MainTex", diffuseTextures[indexPointer]);
//Increment the i Variable.
indexPointer++;
//Check whether the value of indexPointer is equal or bigger than the length of diffuseTextures.
//If yes, then set indexPointer = 0
if(indexPointer >= diffuseTextures.Length){
indexPointer = 0;
}
}
}
Thanks for the response, i will try the code ASAP.
using UnityEngine;
using System.Collections;
using System;
public class SwitchImage: MonoBehaviour{
public Texture2D[] diffuseTextures;
public int indexPointer = 0;
//This is just part of my old code :sweat_smile:
private bool start = false;
public void DoTextureSwitch()
{
//set the texture according to the array
renderer.material.SetTexture("_MainTex", diffuseTextures[indexPointer]);
//Increment the i Variable.
indexPointer++;
//Check whether the value of indexPointer is equal or bigger than the length of diffuseTextures.
//If yes, then set indexPointer = 0
if(indexPointer >= diffuseTextures.Length){
indexPointer = 0;
}
}
}
The texture change when another object is clicked.
For example
When you click on a Square object, it will imediately or triggered changes the texture of a Plane object.
But it still does not work
The code below is the trigger. This will be attached to a square object.
using UnityEngine;
using System.Collections;
public class Calling : MonoBehaviour {
//Create an Object reference named switchImage
SwitchImage switchImage=new SwitchImage();
// Update is called once per frame
public void OnMouseDown () {
switchImage.DoTextureSwitch();
}
}
The code below attached to the plane object which will be affected the the code above.
using UnityEngine;
using System.Collections;
using System;
public class SwitchImage: MonoBehaviour{
public Texture2D[] diffuseTextures;
public int indexPointer = 0;
public void DoTextureSwitch()
{
//set the texture according to the array
renderer.material.SetTexture("_MainTex", diffuseTextures[indexPointer]);
//Increment the i Variable.
indexPointer++;
//Check whether the value of indexPointer is equal or bigger than the length of diffuseTextures.
//If yes, then set indexPointer = 0
if(indexPointer >= diffuseTextures.Length){
indexPointer = 0;
}
}
}