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?

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

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

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

YEAH I DID IT :smiley:
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;
			}
		}
	
	}
}