Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Juancho · Jun 17, 2013 at 04:39 PM · materialchangeonmousedrag

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?.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jun 17, 2013 at 05:12 PM 0
Share

Please post your code. without your code, we would only be guessing about your issue. $$anonymous$$ake sure the shader you use in your material supports transparency. In addition you might be able to just change the main color property rather than change the whole material.

avatar image 000050 · Dec 05, 2013 at 09:28 AM 0
Share

How to drag and drop ? Do you have a video tutorial?

[1]: /storage/temp/18919-draganddrop.jpg

draganddrop.jpg (204.7 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jaxcap · Jun 17, 2013 at 05:24 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Juancho · Jun 17, 2013 at 07:52 PM

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),"");
         
         CheckButton(boton1,1);
         CheckButton(boton2,2);
         GUI.Button(boton1,"");
         GUI.Button(boton2,"");
         
         GUI.EndGroup();
 
     }
     
     void CheckButton(Rect boton,int cod) {
         
         switch(cod) {
             case 1:obj=(GameObject) Resources.Load("Cube01");break;
             case 2:obj=(GameObject) Resources.Load("Cube02");break;
         }
         
         ObjectToPlace=obj.transform;
         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;
                  
                 }else{
                  
                     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))) {
                 Destroy(clone.gameObject);
                 dest=true;
             }
             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() {
         textureColor=renderer.material;
         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) {
             renderer.material=textureColor;
         }
     }
 }

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

NullReferenceException 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 :(.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

change a material from multiple materials 1 Answer

Change Material in other Scene by Scripting 0 Answers

Changing gameObjects material on trigger? 1 Answer

How to change the colour of my car? 1 Answer

how do you change a skybox material by name 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges