- Home /
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?
I'm new on unity... So I don't really know even if this question can be seen by other people
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
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.
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;
}
}
}
}
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
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?)`
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?)`
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
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
It didn't work too :s
It says:
Assets/Scripts/mudarcor.cs(24,25): error CS0030: Cannot convert type `UnityEngine.SpriteRenderer' to `UnityEngine.Sprite'
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'
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.
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
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
Well yeah... $$anonymous$$y objetive was to change the one I click on. It didn't work :s The error is:
Thanks for helping. Sorry for bothering so much. If I have more doubts I will ask (:
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
Follow this Question
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