How to change 2D sprite object by clicking? and it will change back when clicking another object, like focus...
up vote
-2
down vote
favorite
I wish to find out how to change a object (sprite) by clicking (then it will change to another sprite object).
If I click another object, the previous changed object will then changed back, and the newly clicked object will be changed to another object.
unity3d
New contributor
add a comment |
up vote
-2
down vote
favorite
I wish to find out how to change a object (sprite) by clicking (then it will change to another sprite object).
If I click another object, the previous changed object will then changed back, and the newly clicked object will be changed to another object.
unity3d
New contributor
Welcome to stack overflow! Please note anything other than a question is irrelevant information. Putting "urgent" in the question is not only unnecessary, it will also make people avoid your question. Please also specify what you have tried to solve your problem, and what the problems are you are running into.
– ikkentim
22 hours ago
ok, I am sorry, I will avoid it next time.
– Eric0x
22 hours ago
This could help: gamedev.stackexchange.com/questions/72765/…
– Andrea ジーティーオー
22 hours ago
Please Take the Tour , and be sure to read How do I ask a good question? try to give a detailed information about your question. What issue your facing? , where your blocking? , what you tried so far? like the way you have to ask your question.
– Agilanbu
22 hours ago
1
thanks Andrea, it really helps
– Eric0x
22 hours ago
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I wish to find out how to change a object (sprite) by clicking (then it will change to another sprite object).
If I click another object, the previous changed object will then changed back, and the newly clicked object will be changed to another object.
unity3d
New contributor
I wish to find out how to change a object (sprite) by clicking (then it will change to another sprite object).
If I click another object, the previous changed object will then changed back, and the newly clicked object will be changed to another object.
unity3d
unity3d
New contributor
New contributor
edited 12 hours ago
halfer
14.2k757105
14.2k757105
New contributor
asked 22 hours ago
Eric0x
1
1
New contributor
New contributor
Welcome to stack overflow! Please note anything other than a question is irrelevant information. Putting "urgent" in the question is not only unnecessary, it will also make people avoid your question. Please also specify what you have tried to solve your problem, and what the problems are you are running into.
– ikkentim
22 hours ago
ok, I am sorry, I will avoid it next time.
– Eric0x
22 hours ago
This could help: gamedev.stackexchange.com/questions/72765/…
– Andrea ジーティーオー
22 hours ago
Please Take the Tour , and be sure to read How do I ask a good question? try to give a detailed information about your question. What issue your facing? , where your blocking? , what you tried so far? like the way you have to ask your question.
– Agilanbu
22 hours ago
1
thanks Andrea, it really helps
– Eric0x
22 hours ago
add a comment |
Welcome to stack overflow! Please note anything other than a question is irrelevant information. Putting "urgent" in the question is not only unnecessary, it will also make people avoid your question. Please also specify what you have tried to solve your problem, and what the problems are you are running into.
– ikkentim
22 hours ago
ok, I am sorry, I will avoid it next time.
– Eric0x
22 hours ago
This could help: gamedev.stackexchange.com/questions/72765/…
– Andrea ジーティーオー
22 hours ago
Please Take the Tour , and be sure to read How do I ask a good question? try to give a detailed information about your question. What issue your facing? , where your blocking? , what you tried so far? like the way you have to ask your question.
– Agilanbu
22 hours ago
1
thanks Andrea, it really helps
– Eric0x
22 hours ago
Welcome to stack overflow! Please note anything other than a question is irrelevant information. Putting "urgent" in the question is not only unnecessary, it will also make people avoid your question. Please also specify what you have tried to solve your problem, and what the problems are you are running into.
– ikkentim
22 hours ago
Welcome to stack overflow! Please note anything other than a question is irrelevant information. Putting "urgent" in the question is not only unnecessary, it will also make people avoid your question. Please also specify what you have tried to solve your problem, and what the problems are you are running into.
– ikkentim
22 hours ago
ok, I am sorry, I will avoid it next time.
– Eric0x
22 hours ago
ok, I am sorry, I will avoid it next time.
– Eric0x
22 hours ago
This could help: gamedev.stackexchange.com/questions/72765/…
– Andrea ジーティーオー
22 hours ago
This could help: gamedev.stackexchange.com/questions/72765/…
– Andrea ジーティーオー
22 hours ago
Please Take the Tour , and be sure to read How do I ask a good question? try to give a detailed information about your question. What issue your facing? , where your blocking? , what you tried so far? like the way you have to ask your question.
– Agilanbu
22 hours ago
Please Take the Tour , and be sure to read How do I ask a good question? try to give a detailed information about your question. What issue your facing? , where your blocking? , what you tried so far? like the way you have to ask your question.
– Agilanbu
22 hours ago
1
1
thanks Andrea, it really helps
– Eric0x
22 hours ago
thanks Andrea, it really helps
– Eric0x
22 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
How to Change Sprite
private void changeSprite(Sprite newSprite){
this.GetComponent<SpriteRenderer>().sprite = newSprite;
}
//if the object is an image you should use this instead
private void changeSprite(Sprite newSprite){
this.GetComponent<Image>().overrideSprite = newSprite;
}
How to get it by "Clicking"
private void OnMouseDown()
{
changeSprite();
}
//If it's a UI Element
public Button btn;
private void Start()
{
btn.onClick.AddListener(changeSprite);
}
The "swap stuff" effect, note that this is "static" for only 2 objects, you need to iterate over a list/array or whatever to works dynamically independant over the number of objects.
//Your Object Class
public Sprite initialSprite;
public Sprite changeSprite;
public bool isChanged = false;
public GameObject otherObject;
private void OnMouseDown()
{
changeSprite(changeSprite);
isChanged = !isChanged;
if(otherObject.isChanged)
{
otherObject.changeSprite(initialSprite);
otherObject.isChanged = !otherObject.isChanged;
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
How to Change Sprite
private void changeSprite(Sprite newSprite){
this.GetComponent<SpriteRenderer>().sprite = newSprite;
}
//if the object is an image you should use this instead
private void changeSprite(Sprite newSprite){
this.GetComponent<Image>().overrideSprite = newSprite;
}
How to get it by "Clicking"
private void OnMouseDown()
{
changeSprite();
}
//If it's a UI Element
public Button btn;
private void Start()
{
btn.onClick.AddListener(changeSprite);
}
The "swap stuff" effect, note that this is "static" for only 2 objects, you need to iterate over a list/array or whatever to works dynamically independant over the number of objects.
//Your Object Class
public Sprite initialSprite;
public Sprite changeSprite;
public bool isChanged = false;
public GameObject otherObject;
private void OnMouseDown()
{
changeSprite(changeSprite);
isChanged = !isChanged;
if(otherObject.isChanged)
{
otherObject.changeSprite(initialSprite);
otherObject.isChanged = !otherObject.isChanged;
}
}
add a comment |
up vote
1
down vote
How to Change Sprite
private void changeSprite(Sprite newSprite){
this.GetComponent<SpriteRenderer>().sprite = newSprite;
}
//if the object is an image you should use this instead
private void changeSprite(Sprite newSprite){
this.GetComponent<Image>().overrideSprite = newSprite;
}
How to get it by "Clicking"
private void OnMouseDown()
{
changeSprite();
}
//If it's a UI Element
public Button btn;
private void Start()
{
btn.onClick.AddListener(changeSprite);
}
The "swap stuff" effect, note that this is "static" for only 2 objects, you need to iterate over a list/array or whatever to works dynamically independant over the number of objects.
//Your Object Class
public Sprite initialSprite;
public Sprite changeSprite;
public bool isChanged = false;
public GameObject otherObject;
private void OnMouseDown()
{
changeSprite(changeSprite);
isChanged = !isChanged;
if(otherObject.isChanged)
{
otherObject.changeSprite(initialSprite);
otherObject.isChanged = !otherObject.isChanged;
}
}
add a comment |
up vote
1
down vote
up vote
1
down vote
How to Change Sprite
private void changeSprite(Sprite newSprite){
this.GetComponent<SpriteRenderer>().sprite = newSprite;
}
//if the object is an image you should use this instead
private void changeSprite(Sprite newSprite){
this.GetComponent<Image>().overrideSprite = newSprite;
}
How to get it by "Clicking"
private void OnMouseDown()
{
changeSprite();
}
//If it's a UI Element
public Button btn;
private void Start()
{
btn.onClick.AddListener(changeSprite);
}
The "swap stuff" effect, note that this is "static" for only 2 objects, you need to iterate over a list/array or whatever to works dynamically independant over the number of objects.
//Your Object Class
public Sprite initialSprite;
public Sprite changeSprite;
public bool isChanged = false;
public GameObject otherObject;
private void OnMouseDown()
{
changeSprite(changeSprite);
isChanged = !isChanged;
if(otherObject.isChanged)
{
otherObject.changeSprite(initialSprite);
otherObject.isChanged = !otherObject.isChanged;
}
}
How to Change Sprite
private void changeSprite(Sprite newSprite){
this.GetComponent<SpriteRenderer>().sprite = newSprite;
}
//if the object is an image you should use this instead
private void changeSprite(Sprite newSprite){
this.GetComponent<Image>().overrideSprite = newSprite;
}
How to get it by "Clicking"
private void OnMouseDown()
{
changeSprite();
}
//If it's a UI Element
public Button btn;
private void Start()
{
btn.onClick.AddListener(changeSprite);
}
The "swap stuff" effect, note that this is "static" for only 2 objects, you need to iterate over a list/array or whatever to works dynamically independant over the number of objects.
//Your Object Class
public Sprite initialSprite;
public Sprite changeSprite;
public bool isChanged = false;
public GameObject otherObject;
private void OnMouseDown()
{
changeSprite(changeSprite);
isChanged = !isChanged;
if(otherObject.isChanged)
{
otherObject.changeSprite(initialSprite);
otherObject.isChanged = !otherObject.isChanged;
}
}
answered 22 hours ago
Lotan
945114
945114
add a comment |
add a comment |
Eric0x is a new contributor. Be nice, and check out our Code of Conduct.
Eric0x is a new contributor. Be nice, and check out our Code of Conduct.
Eric0x is a new contributor. Be nice, and check out our Code of Conduct.
Eric0x is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372106%2fhow-to-change-2d-sprite-object-by-clicking-and-it-will-change-back-when-clickin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to stack overflow! Please note anything other than a question is irrelevant information. Putting "urgent" in the question is not only unnecessary, it will also make people avoid your question. Please also specify what you have tried to solve your problem, and what the problems are you are running into.
– ikkentim
22 hours ago
ok, I am sorry, I will avoid it next time.
– Eric0x
22 hours ago
This could help: gamedev.stackexchange.com/questions/72765/…
– Andrea ジーティーオー
22 hours ago
Please Take the Tour , and be sure to read How do I ask a good question? try to give a detailed information about your question. What issue your facing? , where your blocking? , what you tried so far? like the way you have to ask your question.
– Agilanbu
22 hours ago
1
thanks Andrea, it really helps
– Eric0x
22 hours ago