- Home /
Problem wiht RaycastHit2D ! Changing hit's sprite
Hello
I want to change hit's sprite that is in front of my player(2D) with raycast2d
But I have many objects that are the same(have one tag and name and layer) and I want that change the sprite of any these game objects that hit.
I've set the layer just for these game objects. but this code do what I want just for the first hit!!
here is my code Thanks
function WaitForGh (waitTime : float) {
yield WaitForSeconds (waitTime);
ghermez = false;
}
function Update () {
var hit: RaycastHit2D = Physics2D.Raycast(transform.position, Vector2.right,20,layer);
if (hit)
{
if(hit.collider.transform.gameobject.GetComponent(SpriteRenderer).sprite == "Ghermez")
print(hit.transform.name);
if(ghermez){
WaitForGh(1.5);
hit.collider.transform.gameObject.GetComponent(SpriteRenderer).sprite = sabz;
}
}
i guess this line contains error
if(hit.collider.transform.gameobject.GetComponent(SpriteRenderer).sprite == "Ghermez") ........
you are asking if type Sprite is equal to "Ghermez" which is string value - maybe you are asking hame, then if(hit.collider.transform.gameobject.GetComponent(SpriteRenderer).sprite.name == "Ghermez")
Answer by AMIR_REZAs · Aug 20, 2014 at 03:48 PM
Finally solved I did it
if(hit){
if(hit.collider.transform.gameObject.GetComponent(SpriteRenderer).sprite == sabz)
ghermez = false;
if(hit.collider.transform.gameObject.GetComponent(SpriteRenderer).sprite != sabz)
ghermez = true;
Thanks
Thanks komodor.
Your answer
![](https://koobas.hobune.stream/wayback/20220613161041im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast doesn't update 1 Answer
Instantiate as a child? 3 Answers
Raycast lines debug problem 1 Answer
Raycast only returning parent object; need child hit 3 Answers