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.










share|improve this question









New contributor




Eric0x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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















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.










share|improve this question









New contributor




Eric0x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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













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.










share|improve this question









New contributor




Eric0x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Eric0x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Eric0x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 12 hours ago









halfer

14.2k757105




14.2k757105






New contributor




Eric0x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 22 hours ago









Eric0x

1




1




New contributor




Eric0x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Eric0x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Eric0x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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










  • 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












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;
}
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Eric0x is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    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

























    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;
    }
    }





    share|improve this answer

























      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;
      }
      }





      share|improve this answer























        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;
        }
        }





        share|improve this answer












        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;
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 22 hours ago









        Lotan

        945114




        945114






















            Eric0x is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            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.















             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$