- Home /
Not able to Get clicked sprite name in this code
Hi all,
I have following code. I am trying to get clicked sprite name at run time. I have added rigidbody2d and boxcolider2d but I am not getting name.. Attached image public class test : MonoBehaviour {
public Sprite[] card;
public List <Sprite> items;
public Transform start;
public float howManyAdded; // How many cards I added so far
float gapFromOneItemToTheNextOne; //the gap I need between each card
void Awake()
{
// load all frames in cards array
card = Resources.LoadAll<Sprite>("CardDeck_");
}
void Start()
{
howManyAdded = 0.0f;
gapFromOneItemToTheNextOne = 0.2f;
GameObject cards = new GameObject();
GameObject cardPart1 = new GameObject();
//cardPart1.SetParent(cards.transform);
cardPart1.transform.position = start.position;
cardPart1.transform.position += new Vector3 (( howManyAdded*gapFromOneItemToTheNextOne), 0, 0);
cardPart1.transform.Rotate( 0f, 0f, -6f );
cardPart1.transform.SetParent(cards.transform);
howManyAdded++;
cardPart1.AddComponent<SpriteRenderer>();
cardPart1.GetComponent<SpriteRenderer>().sprite = card[8];
GameObject cardPart2 = new GameObject();
//cardPart2.SetParent(cards.transform);
//cardPart2.transform.SetParent ();
cardPart2.transform.position = start.position;
cardPart2.transform.position += new Vector3 (( howManyAdded*gapFromOneItemToTheNextOne), 0, 0);
cardPart2.transform.Rotate( 0f, 0f, -6f );
cardPart2.transform.SetParent(cards.transform);
//Rigidbody2D gameObjectsRigidBody = cardPart2.AddComponent<Rigidbody2D>();
//gameObjectsRigidBody.
//gameObjectsRigidBody.isKinematic = true;
cardPart2.AddComponent<SpriteRenderer>();
cardPart2.GetComponent<SpriteRenderer>().sprite = card[15];
}
void OnMouseDown(){
Debug.Log("Sprite Clicked");
}
}
Answer by Cuttlas-U · Oct 24, 2017 at 08:37 AM
hi; the code seems fine; so i think problem can be from inspector or hirachy ; can u show me some screen shot from those ?
you colloder size is too small and it already giving u a warning to make it bigger; Reset the collider component or change its size to 1 on both x and y direction so u can see the green lines of collider on the screen and check again ;
still not able to find the name and not even detecting the click
Your answer
Follow this Question
Related Questions
Cant access the Image icon from class 1 Answer
Orbital cannon jitters while following/facing mouse position 1 Answer
Cant acces an image icon from class 0 Answers
How to Create a Launch Path? 2 Answers