Search code examples
c#unity-game-engineunityscriptaugmented-realityvuforia

How to change the texture of object at run time on button click in unity by using AR camera


I want to change the textures of 3D model at run time in unity by using AR camera.
I have done this work by using main camera but by using AR camera its not working.
I attach this script to a cube and create a button and add the cube in onClick function of button and its work fine by using unity Main camera but its not working with AR Camera. Here's my script:

using UnityEngine;using System.Collections;public class TexturesSwap : MonoBehaviour {
public Texture[] textures;
public int currentTexture;

void Start () {

}   
public void swapTexture() { currentTexture++;
    currentTexture %= textures.Length;
    GetComponent<Renderer>().material.mainTexture = textures[currentTexture]; 
}}

For swap textures I have textures folder with some textures and through script I am able to change the texture of object when I am using Main camera but how to do the same thing by using AR camera? Please help me. Thanks.

Here's my errorenter image description here


Solution

  • You need to add a Renderer to object. For this..Create a material in assets (right click on assets folder and click material). Attach this material to object. And then set texture in code:

      transform.GetComponent<Renderer>().material.texture="yourTexture"