Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am working on unity2d. I have six game objects:

Object1, Object2, Object3 (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

This is similar to selection. My code is as follows:

public GameObject newSprite; 
private Vector3 currentSpritePosition; 

void Start()
{ 
    newSprite.renderer.enabled = false; //then make it invisible 
    currentSpritePosition = transform.position; //give the new sprite the position of the latter 
    renderer.enabled = false; //then make it visible 
    newSprite.transform.position = currentSpritePosition; 
    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;
}

Currently when I click on Object2 it changes to ObjectImage2 but the first object is not changing to Object1 from ObjectImage1.

share|improve this question
    
At no point are you actually changing any materials. What you're actually doing is enabling and disabling entire game objects to simulate material changing. I have changed your title accordingly. –  Nick Udell Aug 26 at 14:44
    
How are your components set up on your game objects? Currently you've shown code for swapping visibility of two game objects on mouse over, which seems fine, but you're not showing us how it's hooked up in Unity. –  Nick Udell Aug 26 at 14:51
1  
How are you expecting onMouseEnter() to handle clicks? You probably want onMouseUp(). –  layzrr Aug 27 at 14:48

1 Answer 1

This is a selection functionality but you could easily include your own rules.

First, it's important that you define a tag for the selection group. In case there are more than one selection group in a scene at a time, there should be more than one tag defined for the groups.

SelectionButton.cs

using UnityEngine;
using System.Collections;

public class SelectionButton : MonoBehaviour
{
    public string selectGroupTag;
    public Sprite spriteDisabled;
    public Sprite spriteEnabled;
    private SpriteRenderer renderer;
    private GameObject[] selectGroup;

    void Start()
    {
        renderer = gameObject.GetComponent<SpriteRenderer>();
        renderer.sprite = spriteDisabled;
        selectGroup = GameObject.FindGameObjectsWithTag(selectGroupTag);
    }

    public void SetEnabled(bool enabled)
    {
        if (enabled)
            renderer.sprite = spriteEnabled;
        else
            renderer.sprite = spriteDisabled;
    }

    void OnMouseUpAsButton()
    {
        foreach (GameObject o in selectGroup)
            o.GetComponent<SelectionButton>().SetEnabled(false);
        SetEnabled(true);
        // TODO
        // add your set of rules here
    }
}

It's important that you also add a collider component for the code to work properly, and assign all the public variables.

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.