Changing the material on an object on click in unity

Posted by user1509674 on Game Development See other posts from Game Development or by user1509674
Published on 2014-07-19T05:17:38Z Indexed on 2014/08/22 16:38 UTC
Read the original article Hit count: 336

Filed under:
|
|
|

Iam working on unity2d.I have six game object

Object1,Object1,Object1,(these are images)

ObjectImage1,ObjectImage2,ObjectImage3(these are images).

I have arranged the object in the scene as a list one below another

Object1

Object2

Object3

When I click the

Object1 ---> should change to ObjectImage1

Object2 ---->should change to ObjectImage2, but the above image of object1(objectImage1) at present should change to Object1

Object3 ----? should change to ObjectImage3,but the above image on object2(objectImage2) should change to Object2

These is similar to selection.I have coded Like when I click of Object2 its changing to ObjectIamge2 but the first object is not changing to object1 from objectImage1.Can anybody help me coding it out.

Edit:

public GameObject newSprite; 
private Vector3 currentSpritePosition; 

void Start()
{ 
    newSprite.renderer.enabled = false;
    currentSpritePosition = transform.position; //then make it invisible 
    renderer.enabled = false; //give the new sprite the position of the latter 
    newSprite.transform.position = currentSpritePosition; //then make it visible 
    newSprite.renderer.enabled = true; } void OnMouseExit(){ //just the reverse process 
    renderer.enabled = true; 
    newSprite.renderer.enabled = false; 
 } 

This is the code used to change the material: public GameObject newSprite;

private Vector3 currentSpritePosition;

void Start(){

    newSprite.renderer.enabled = false;
}

void OnMouseEnter(){
    //getting the current position of the current sprite if ever it can move;
    currentSpritePosition = transform.position;

    //then make it invisible
    renderer.enabled = false;

    //give the new sprite the position of the latter
    newSprite.transform.position = currentSpritePosition;

    //then make it visible
    newSprite.renderer.enabled = true;
}

void OnMouseExit(){
    //just the reverse process
    renderer.enabled = true;
    newSprite.renderer.enabled = false;
}

© Game Development or respective owner

Related posts about c#

Related posts about unity