Changing Textures via Script (Solved)

Solution

For whatever reason the texture/material for my object was stored in the armature hierarchy (“orbSwitch > orb_armature > pedestal”) so when I applied the script to change it to that part of the object (“pedestal”) it began to work. I figured that having a texture in the parent (“orb switch”) would over-ride the texture/material underneath it, but that is apparently an incorrect assumption. So my script just wasn’t being specific enough to find where the active texture was in order to change it.

Previous Problem

I have a switch that I would like to change textures on when it is hit by the player. You can see it in the image. On the right is the inactive, yellow, switch and on the left is the active, green, switch. Both switches are exactly the same, except for their textures and the fact that the green one is animated to spin when hit.

alt text

So far I’ve tried changing it via materials and via textures and neither seems to update the texture. When I hit play it stays the original yellow texture. Since I figured the animation and trigger testing might be complicating things I went to a test scene and started messing around with only the non-animating switch to see if I could even change its texture via a script.

I added in two variables which are linked to the two texture files through the inspector (both textures are the same except for the color of the top part of the texture). Then I tried using the mainTexture command as suggested for changing textures in the first example from the script reference page. This results in no change.

I’ve also tried using “renderer.material.SetTexture(”_MainTex", textureOn);" with no results.

There was also an example somewhere using an array to cycle through materials and I couldn’t get it to work which is why I have multiple materials set on the object as you can see in the bottom right of the image.

So in summary, how do I get it to change materials or textures if the commands listed in the scripting reference don’t seem to do anything when I hit play?

Edit Oh I see its already been solved, HA SILLY ME!! anyways i’ll leave this solution anyways that way we have two answers! :smiley: Namaste!

Although this question is like 2 years old, maybe I’ll answer it as my solution might work for you? Its just a simple button script that will switch out the texture on the current Material, that way you don’t have to create huge amounts of materials for your textures. Im not sure if this is preformance heavy or not, i dont notice any problems on my android when playing… good luck hope it helps, sorry for being late! :stuck_out_tongue: hahaha

using UnityEngine;
using System.Collections;

public class MasterButton: MonoBehaviour 
{
	public bool isButtonSelected = false;

    //The Textures you wish to use for swaping ( could use a Texture2D[] if you have lots)
	public Texture2D onTexture;
	public Texture2D offTexture;
	//The MeshRenderer of the object your trying to swap textures on
	public MeshRenderer currentRenderer;
	
	
	// Use this for initialization
	void Start () 
	{
    //Find the MeshRenderer Component (could also use FindObjectOfType() )
        currentRenderer = this.GetComponent<MeshRenderer>();
	}
	
	// Update is called once per frame
	void Update () 
	{
        if(currentRenderer != null)
        {
	   	    if(isButtonSelected)
		    	currentRenderer.material.SetTexture("_MainTex", onTexture);
		    else
			    currentRenderer.material.SetTexture("_MainTex", offTexture);
	    }

//You can use this function to change the “isButtonSelected” flag or call some kind of OnCollisionEnter/OnTriggerEnter function, or your own or w/e; either way, switch the flag to cause Update to switch the texture fairly simple

	void OnMouseUpAsButton()
	{
		if(!isButtonSelected)
			isButtonSelected = true;
		else
			isButtonSelected = false;
	}
}