dynamically changing texture of an object
I'm trying to make a GUI button that provides me change the texture of a GameObject... Any ideas?
I tried to use this code, but somehow when I click on button the sphere turns white, no texture...
using UnityEngine;
using System.Collections;
namespace Vuforia
{
public class GUI_Button : MonoBehaviour
{
GameObject Pokeball;
Texture3D texture = (Texture3D) Resources.Load ("Resources/clouds1024");
private void OnGUI()
{
if (GUI.Button (new Rect (5, 15, 100, 25), "Pokebola")) {
Pokeball = GameObject.Find("Pokeball");
Pokeball.GetComponent<Renderer>().material.mainTexture = texture;
}
}
}
}
Comment
I've already have changed this, when I click the button the sphere still co$$anonymous$$g white
Best Answer
Answer by Positive7 · Sep 14, 2015 at 06:30 PM
Resources.Load already looking inside Resources folder so your Texture was null (unless you've another Resources folder inside Resources)
GameObject Pokeball;
Texture2D texture;
void Start(){
texture = (Texture2D) Resources.Load ("clouds1024");
Pokeball = GameObject.Find("Pokeball");
}
private void OnGUI()
{
if (GUI.Button (new Rect (5, 15, 100, 25), "Pokebola")) {
Pokeball.GetComponent<Renderer>().material.mainTexture = texture;
}
}
}