change materials onmousedrag

Hello, i have this problem i can’t solve…

I have a drag and drop GUI, i can drag a box from my gui and drop it in my scene… the problem is… i want my new box to have a semitransparent alpha while on drag but return it to its normal material on drop.

I tried with 2 mats on the Mesh Renderer but it doesn’t work the mat swap.

Any idea?.

Hi! Here’s an example of something easy you could do. Attach the “original” material to the object (through the mesh renderer), and the “new” material to a public variable in a script. Then just switch between the two in the code. I did a quick test and it seemed to work. Here’s a little example:

using UnityEngine;
using System.Collections;

public class MatSwitchTest : MonoBehaviour {
	public Material newMat;
	Material oldMat;
	void Start() {
		oldMat = this.renderer.material;

	void OnMouseDrag () {
		this.renderer.material = newMat;
	void OnMouseUp () {
		this.renderer.material = oldMat;

This is if you want to swap the entire material like you asked. Maybe in your situation, though, you can just switch back and forth from a shader that doesn’t do transparency to one that does. It’s pretty similar.

Sorry… ill try to be more accurate…

Im doing a tower defense game, and i have a interactive GUI which let me drag the “towers” from the GUI to the scene. The code that manages that is:

using UnityEngine;
using System.Collections;

public class Gui : MonoBehaviour {
	public Transform clone;
	public Transform ObjectToPlace;
	private GameObject obj;

	private Vector3 screenPoint;
	private Vector3 offset;
	private Vector3 mousePos;
	private Ray ray2;
	private RaycastHit hit2;
	int layerMask2;
	public bool dest=false;
	private Rect boton1=new Rect(10, 40, 50, 80);
	private Rect boton2=new Rect(70, 40, 50, 80);
    void OnGUI() {
		GUI.BeginGroup(new Rect(0, Screen.height-100, Screen.width, 100));
		GUI.Box(new Rect(0, 0, Screen.width, 100),"");

	void CheckButton(Rect boton,int cod) {
		switch(cod) {
			case 1:obj=(GameObject) Resources.Load("Cube01");break;
			case 2:obj=(GameObject) Resources.Load("Cube02");break;
		if(boton.Contains(Event.current.mousePosition)) {
			if(Input.GetButtonDown("Fire1")) {
				Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
				RaycastHit hit;
				var layerMask = 1 << 8;
				if (Physics.Raycast (ray, out hit, Mathf.Infinity, layerMask)) {
					clone = hit.transform;
					layerMask = ~layerMask;
					if (Physics.Raycast (ray, out hit, Mathf.Infinity, layerMask)) {
						clone = (Transform) Instantiate(ObjectToPlace, hit.point, Quaternion.identity);
		if(Input.GetButtonUp("Fire1")) {
			if ((!dest)&&(Physics.Raycast(ray2, out hit2, Mathf.Infinity, layerMask2))) {
			clone=null; // clear clone variable
		if(Input.GetButton("Fire1")) {
			if (clone!=null) {
				ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
				layerMask2 = 1 << 8;
				layerMask2 = ~layerMask2;
				if (Physics.Raycast (ray2, out hit2, Mathf.Infinity, layerMask2))  {
					clone.position = hit2.point; 
				layerMask2 = 1 << 10;


dont pay attention to the GetButtonUp() function, the destroy was just for testing.

the problem comes now…

my cubes have this script:

using UnityEngine;
using System.Collections;

public class Build : MonoBehaviour {
	private Material textureColor;
	public Material greenColor;
	void Start() {
		greenColor=new Material (Shader.Find("greenTrans"));
	void OnTriggerStay(Collider other) {
		if(other.gameObject.tag==Tags.validSpot) {
        	renderer.material = greenColor;
	void OnTriggerExit(Collider other) {
		if(other.gameObject.tag==Tags.validSpot) {

The Cubes have a red material, and i have another material with Transparency/difusse named “greenTrans” but i get an error in runtime:

UnityEngine.Material…ctor (UnityEngine.Shader shader) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/Graphics.cs:1584)
Build.Start () (at Assets/Scripts/Build.cs:13)

I know… i ain’t getting the Material from my Assets.

The box needs to collide with a Mesh collider but seems it isnt colliding with it.

Sorry about my english… i’m trying to be as accurate as i can :(.