HELP ME with changing materials on mouse click!! :)

Hi guys! I am an architect and I have very little background in scripting. I have searched the forums for the last 3 hours and I did not find a complete solution to my problem. I have seen that there are people here that know a lot about scripting for unity and that are willing to teach others so … this is what I need to achieve:

I need the walls in my “game” or the floors or some furniture to change the materials or textures (I think the materials is better so I can have normal maps in them for a more realistic feeling) when I click with the mouse on a wall or a floor or a piece of furniture. I have seen this done on many architectural visualizations done with unity but I need this explained to me … from “start” to “end” … So… may be someone want to explain to me on a simple scene with 2 cubes, every one changing 2-3 materials or textures when I click the cubes with the mouse. I have already did this with uScript but uScript don’t compile for flash (unity 3.5) but I need to export in flash … so learning to do it in native unity scripting is the solution I think…

Thank You!!! :slight_smile:

Ok, a basic implementation:

  1. Create a script called MaterialChanger.cs or whatever you want to call it.

  2. Create a function OnMouseDown()

  3. Define a public Material (e.g. material1) so you can drag and drop it in the editor

  4. In OnMouseDown(), put the line renderer.material = material1;

As long as the object in your scene has a collider and this script (and has a different material to the one specified in the script), it should change material when you click on it. Hopefully this will give you enough to start with and expand from there.

thank you! it is the first step that I need… others are welcome to came with suggestions and tips :smile:

if I learn how to do this I’ll make a videotutorial :smile:… I think newbs like me will want that ;))

ok… so using your suggestions I made this to work with 2 materials, the code will switch the existing material with material1 … how do I make a scrip that toggle 3 or more materials on mouse click on the object?

I have found a code that do that but will aply the change on any click not if I click on the object… another sidefect is that if I apply the script to different objects (and define different arrays and different mat) all objects will change their materials on any mouse click. the code is:

var myMaterials : Material[];
var NextMaterial : int = 0;


function NextMaterialUsed()
{
	renderer.sharedMaterial = myMaterials[NextMaterial];
		if(NextMaterial < myMaterials.length-1)
			NextMaterial += 1;
		else
			NextMaterial = 0;
}



function Update () 
{
	if(Input.GetButtonDown("Fire1"))
	{
		NextMaterialUsed();
	}
}

I have found it on Unity Answers…

now… I want to toggle a number of materials , more then 2, if I click the object that has to change, not if I just click anywhere …

in the above code what should I use instead of Input.GetButtonDown("Fire1") to tell the script to change the material only if I clicked the object?
Any suggestions? :face_with_spiral_eyes:

something like this might work… no promises as I dont write JS

var myMaterials : Material[];
var NextMaterial : int = 0;

function NextMaterialUsed(obj : GameObject)
{
    obj.renderer.material = myMaterials[NextMaterial];
    NextMaterial++;
    if(NextMaterial > myMaterials.length-1)
       NextMaterial = 0;
}

function Update () 
{

    if(Input.GetButtonDown("Fire1"))
    {
      var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
      var hit : RaycastHit;
       if (Physics.Raycast (ray, hit)) {
            NextMaterialUsed(hit.transform.gameObject);
        }
       
    }
}

@ Jlcnz :

the script works …it changes materials BUT :smile: it changes the materials of any object that I point and fire at :smile:…I have a plane and 3 objects (a sphere, a cube and a capsule) and I attached the script only to the capsule. my array contains 4 materials. when I point to the sphere and fire it changes the materials of the sphere, the same with any object in the scene… I think that in the raycast part of the script I need an “if/else” or a “for”, may be, to check if the ray hit the desired object :smile:… I’ll bump my head on this wall now :)) but if you have a tip :smile:…just post it

Thank you!

ok… I have put the NextMaterialUsed(hit.transform.gameObject); line under another “if” that verify if the ray collide with the gameObject Capsule (or any object that we want to be a trigger I presume…I’ll test that :smile:) and now the code is working as it should and is looking like this:

   var myMaterials : Material[];
    var NextMaterial : int = 0;
     
    function NextMaterialUsed(obj : GameObject)
    {
        obj.renderer.material = myMaterials[NextMaterial];
        NextMaterial++;
        if(NextMaterial > myMaterials.length-1)
           NextMaterial = 0;
    }
     
    function Update ()
    {
     
        if(Input.GetButtonDown("Fire1"))
        {
          var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
          var hit : RaycastHit;
           if (Physics.Raycast (ray, hit)) {
                if (hit.collider.gameObject.name=="Capsule"){
                NextMaterialUsed(hit.transform.gameObject);}
            }
           
        }
    }

I tried to attach a similar script to another object in the scene but it will not work. it seems that unity will take in consideration only the initial script. I have changed the name of the functions and variables in the second script but it still don’t work. If I change the name of the object in the first script than it will change the materials on the specified object… So how can I make it work on different objects?… is there a way to tell the engine when I hit an object to discard any other functions and take in consideration only the functions in the current script? :smile:

Another problem is: it’s doing what it’s supposed to do only in unity player…in flash when I click on the object it is doing… nothing :)…how do I fix that?

Edit:
Fixed it: it seems that the script name must not contain any spaces or it will not be read by flash…

   var myMaterials : Material[];
    var NextMaterial : int = 0;
   var TargetName : string = "Capsule";
     
    function NextMaterialUsed(obj : GameObject)
    {
        obj.renderer.material = myMaterials[NextMaterial];
        NextMaterial++;
        if(NextMaterial > myMaterials.length-1)
           NextMaterial = 0;
    }
     
    function Update ()
    {
     
        if(Input.GetButtonDown("Fire1"))
        {
          var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
          var hit : RaycastHit;
           if (Physics.Raycast (ray, hit)) {
                if (hit.collider.gameObject.name== TargetName ){
                NextMaterialUsed(hit.transform.gameObject);}
            }
           
        }
    }

This script showing error…can u please share Perfect Script.