Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DavidCoelho · Jan 23, 2015 at 12:09 PM · c#

Changing Sprite OnMouseDown

Hey guys. I'm trying to do the Tic-Tac-Toe game and my idea was doing it with sprites. But I'm stuck and can't keep on because when I try to change the sprite when I click with the mouse, it doesn't work... The objetive it's when I click the mouse it only changes one gameobject and not all of them... Take a look on the code.

 using UnityEngine;
 using System.Collections;
 
 public class mudarcor : MonoBehaviour {
     
     public Sprite bola;
     public Sprite cruz;
     private SpriteRenderer spriteRenderer;
     byte[,] jogo;
 
     // Use this for initialization
     void Start () {
         jogo = new byte[3,3] { { 0, 0, 0 }, { 0, 0, 0 },{ 0, 0, 0 } };
         spriteRenderer = GetComponent<SpriteRenderer>();
 
     }
     
  
     void Update () {
 
 //        if (Input.GetMouseButtonDown (0)) // If the space bar is pushed down
 //        {
 //            ChangeSprite(); // call method to change sprite
 //        }
     }
 
     //void ChangeSprite()

     void onMouseDown()
     {
 
         if (spriteRenderer.sprite = bola) // if the spriteRenderer sprite = sprite1 then change to sprite2
         {
             spriteRenderer.sprite = cruz;
         }
         else
         {
             spriteRenderer.sprite = bola; // otherwise change it back to sprite1
         }
     }
 
 }




Can someone help me pls?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DavidCoelho · Jan 23, 2015 at 11:57 AM 0
Share

I'm new on unity... So I don't really know even if this question can be seen by other people

avatar image DavidCoelho · Jan 26, 2015 at 12:38 PM 0
Share

Sorry guys for giving feedback only now... So I tried both of the solutions you guys gave me (and thanks for trying to help me) and it didn't work... I don't know why, it's my fault for sure, I'm a beginner :s

avatar image stevecus · Jan 26, 2015 at 04:33 PM 0
Share

You need a capital on On$$anonymous$$ouseDown(), if still not working try using as say void SpiteChanger(), add if(input.Get$$anonymous$$ouseButtonUp(0)) and call that in Update.

As a tip make spriteRenderer just sRen or ren to shorten things. Estially ren.sprite = blah is correct so look at your other code.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DavidCoelho · Jan 29, 2015 at 11:39 AM

YEAH I DID IT :D It was a problem with the camera... I changed it to ortographic and it's working now c:

 void Update () {
     
         if(Input.GetMouseButton (0)) 
         {
         
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit2D hit = Physics2D.Raycast(ray.origin,ray.direction);
             
             if(hit != null && hit.collider != null){
                 Debug.Log(hit.collider.gameObject.name);
                 GameObject.Find(hit.collider.gameObject.name).GetComponent<SpriteRenderer>().sprite = textura_bola;
             }
         }
     
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by ankush20 · Jan 23, 2015 at 12:35 PM

Hello David,

Try this way, It will solve your problem. Assuming the script is attached on the gameObject of which you want to change sprite. Assign all the GameObject's with the same Tag:

 var sprite_change : Sprite;// Create a variable of sprite type.
 
 void Update () 
 {
      if (Input.GetMouseButtonDown (0))
      {
           GameObject.FindGameObjectsWithTag("SpriteChange").GetComponent(SpriteRenderer).sprite = sprite_change;    //Get the component sprite and assign it the sprite variable
      }
 }

Thanks Ankush Taneja

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DavidCoelho · Jan 26, 2015 at 11:27 AM 0
Share

I tried it and it didn't work :s

It says:

 `Assets/Scripts/mudarcor.cs(24,75): error CS1061: Type `UnityEngine.GameObject[]' does not contain a definition for `GetComponent' and no extension method `GetComponent' of type `UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)`

avatar image DavidCoelho · Jan 26, 2015 at 12:37 PM 0
Share

So for the first solution, Ankush Taneja, here is the code and it's error:

 using UnityEngine;
  using System.Collections;
  
  public class mudarcor : $$anonymous$$onoBehaviour {
      
      public Sprite textura_bola;
      public Sprite textura_cruz;
      public SpriteRenderer spriteRenderer;
      byte[,] jogo;
      
  
  
      // Use this for initialization
      void Start () {
          jogo = new byte[3,3] { { 0, 0, 0 }, { 0, 0, 0 },{ 0, 0, 0 } };
          spriteRenderer = GetComponent<SpriteRenderer>();
  
      }
      
      // Update is called once per frame
      void Update () {
  
          if (Input.Get$$anonymous$$ouseButtonDown (0))
          {
               GameObject.FindGameObjectsWithTag("SpriteChange").GetComponent(SpriteRenderer).sprite = textura_bola;    //Get the component sprite and assign it the sprite variable
          }
      }
  
  }

 
 

Error

   `Assets/Scripts/mudarcor.cs(24,75): error CS1061: Type `UnityEngine.GameObject[]' does not contain a definition for `GetComponent' and no extension method `GetComponent' of type `UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)`
avatar image ankush20 · Jan 27, 2015 at 06:47 AM 0
Share

Hello $$anonymous$$,

$$anonymous$$ake sure that the gameObject is SpriteType and having sprite component on it. Select the gameObject from hierarchy view and check in inspector view whether it is having sprite component or not.

Thanks

avatar image
0

Answer by shriya · Jan 23, 2015 at 12:48 PM

hi David,

the problem is
private SpriteRenderer spriteRenderer; In this you are assigning one game object and the sprite change is also occuring on the same object. For finding all the objects either you declare array of sprite renderers

 public SpriteRenderer[] spriteRenderer;

and assign all renderers of one tag here and inplace of if statement use

 foreach( sprite x in  spriteRenderer)
 {
  x.GetComponent(SpriteRenderer).sprite = sprite_change;
 }

Other way is you can use FindGameObject WithTag

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DavidCoelho · Jan 26, 2015 at 11:37 AM 0
Share

It didn't work too :s

It says:

 Assets/Scripts/mudarcor.cs(24,25): error CS0030: Cannot convert type `UnityEngine.SpriteRenderer' to `UnityEngine.Sprite'

avatar image DavidCoelho · Jan 26, 2015 at 12:38 PM 0
Share

For the second solution given by shriya here is the code and it's error:

 using UnityEngine;
 using System.Collections;
 
 public class mudarcor : $$anonymous$$onoBehaviour {
     
     public Sprite textura_bola;
     public Sprite textura_cruz;
     public SpriteRenderer[] spriteRenderer;
     byte[,] jogo;
     
 
 
     // Use this for initialization
     void Start () {
         jogo = new byte[3,3] { { 0, 0, 0 }, { 0, 0, 0 },{ 0, 0, 0 } };
 
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.Get$$anonymous$$ouseButtonDown (0)) {
             foreach(Sprite x in spriteRenderer)
             {
                 x.GetComponent(SpriteRenderer).sprite = textura_bola;
             }
         }
     }
 
 }



Error

 Assets/Scripts/mudarcor.cs(24,25): error CS0030: Cannot convert type `UnityEngine.SpriteRenderer' to `UnityEngine.Sprite'



avatar image shriya · Jan 27, 2015 at 06:55 AM 0
Share

hi,

To remove this error do below mentioned code . Earlier I dint check it with Unity but now I have checked and its working.

 if (Input.Get$$anonymous$$ouseButtonDown (0)) {
                 foreach(var x in spriteRenderer)
                 {
 
                     x.GetComponent<SpriteRenderer>().sprite = textura_bola;
                 }
             }

$$anonymous$$ake sure you assign all sprite objects of same tag in the inspector as shown in the image.alt text

screen-shot-2015-01-27-at-122346-pm.png (15.2 kB)
avatar image
0

Answer by DavidCoelho · Jan 27, 2015 at 11:24 AM

Hey guys, thanks for the support (: It worked with this:

 using UnityEngine;
 using System.Collections;
 
 public class mudarcor : MonoBehaviour {
     
     public Sprite textura_bola;
     public Sprite textura_cruz;
     public SpriteRenderer[] spriteRenderer;
     byte[,] jogo;
     
 
 
     // Use this for initialization
     void Start () {
         jogo = new byte[3,3] { { 0, 0, 0 }, { 0, 0, 0 },{ 0, 0, 0 } };
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetMouseButtonDown (0)) {
 
             foreach(SpriteRenderer x in spriteRenderer)
             {
                 x.sprite = textura_cruz;
 
             }
 
         }
     }
 
 }

The only problem now is that it puts every square with the textura_cruz sprite :s

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shriya · Jan 28, 2015 at 05:22 AM 0
Share

I guess your Question is misunderstood . Do you want all objects with same tag to change on mouse Down ? Or Just the one you are clicking.

If you are trying for just the one you are clicking You will have to use Raycast

 if (Input.Get$$anonymous$$ouseButton (0) ) 
         {
 //            
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             if (Physics.Raycast(ray, out hit, 100))
             {
                 if (  hit.collider.tag =="SpriteChange")
                 {
                                     hit.sprite = textura_cruz;
     
                                    }
                         }
 }


I hope this works for you

avatar image DavidCoelho · Jan 29, 2015 at 11:18 AM 0
Share

Well yeah... $$anonymous$$y objetive was to change the one I click on. It didn't work :s The error is: alt text

error.png (30.7 kB)
avatar image DavidCoelho · Jan 29, 2015 at 11:41 AM 0
Share

Thanks for helping. Sorry for bothering so much. If I have more doubts I will ask (:

avatar image shriya · Jan 29, 2015 at 12:16 PM 0
Share

I forgot to declare RaycastHit hit; variable in the code. else it would have worked without changing the camera to orthographic as well.

Glad you could make it work :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

[Answered](C#)Comparing two Lists for a Crafting System 2 Answers

[Answered](C#) Simplex Noise and Negative Coordinates 0 Answers

Keep getting this error CS8025 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges