2D Toolkit Forum

2D Toolkit => Support => Topic started by: Ben on May 07, 2013, 08:37:09 am

Title: Collider and tk2dSprite
Post by: Ben on May 07, 2013, 08:37:09 am
Hi,
Is there any method to retrieve an instance of the class tk2dSprite using a Collider?
By default a collider can return a game object but not a sprite. I'm using the below code to drag sprites, which works nicely, however the object returned by the function hit.collider.gameObject is of type GameObject, not tk2dSprite. If I change the object obj to be of the type tk2dSprite and cast the object returned by hit.collider.gameObject to tk2dSprite the code returns an error. Is there any other method to reference the sprite detected by a collider?

GameObject obj;
if (Input.GetMouseButtonDown(0)) {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;           
            if (Physics.Raycast(ray,out hit))
            {               
                obj = hit.collider.gameObject;
          }
}
Title: Re: Collider and tk2dSprite
Post by: unikronsoftware on May 07, 2013, 09:50:48 am
You can get the tk2dSprite from the gameObject by doing:
tk2dSprite sprite = gameObject.GetComponent<tk2dSprite>();
Title: Re: Collider and tk2dSprite
Post by: Ben on May 07, 2013, 11:12:53 am
Great! Thank you!