Hello Guest

Author Topic: Collider and tk2dSprite  (Read 4168 times)

Ben

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Collider and tk2dSprite
« 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;
          }
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Collider and tk2dSprite
« Reply #1 on: May 07, 2013, 09:50:48 am »
You can get the tk2dSprite from the gameObject by doing:
tk2dSprite sprite = gameObject.GetComponent<tk2dSprite>();

Ben

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Collider and tk2dSprite
« Reply #2 on: May 07, 2013, 11:12:53 am »
Great! Thank you!