- Home /
How can i change sprite of gameObject in the code file of GameController,How can I change sprite of game object in the code file of GameController
public class gameController: MonoBehaviour{
public GameObject balon, balonTarget;
Vector2 balonInitialPos;
void Start() { balonInitialPos= balon.transform.position; }
public void dragBalon(){ balon.transform.postion=Input.mousePosition. }
dropBalon() { float Distance=Vector3.Distance(balon.transform.postion, balonTarget.transform.postion); if(Distance<50){
//here i want to change sprite of targetBalon to another sprite image. } else { balon.tansform.position=balonInitialPos; } }
}
Answer by xxmariofer · Jul 08, 2020 at 01:45 PM
please format your questions, try this code, make sure to fill the inspector field
public class gameController: MonoBehaviour{
public GameObject balon, balonTarget;
Vector2 balonInitialPos;
public Sprite secondSprite;//FILL IN THE INSPECTOR THE SPRITE
void Start() { balonInitialPos= balon.transform.position; }
public void dragBalon(){ balon.transform.postion=Input.mousePosition. }
dropBalon() { float Distance=Vector3.Distance(balon.transform.postion, balonTarget.transform.postion); if(Distance<50){
balonTarget.GetComponent<SpriteRenderer>().sprite = secondSprite;
//here i want to change sprite of targetBalon to another sprite image. } else { balon.tansform.position=balonInitialPos; } }
}
Your answer
Follow this Question
Related Questions
Parent/Child rotation not working 1 Answer
how to make high scores using playerprefs 2 Answers
Block Game Object interaction with another Game Object with kinematic active 0 Answers
Program for 2D drawing? 1 Answer
How can I make this script work ? 0 Answers