Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't use a cursor controller script on multiple objects

Discussion in 'Scripting' started by tytbone, Apr 14, 2015.

  1. tytbone

    tytbone

    Joined:
    Jul 16, 2013
    Posts:
    18
    I'm having issues with this cursor controller script.

    Basically I want the cursor to change if the object it is attached to is visible and of course the cursor is over the object.

    However, the script only works with one object at a time; if it is attached to more than one object, those other objects don't change the cursor.

      1. publicclass cursorController :MonoBehaviour
      2. {
      3. voidUpdate()
      4. {
      5. CursorMode cursorMode =CursorMode.Auto;
      6. Vector2 hotspot =Vector2.zero;
      7. RaycastHit2D hit =Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition),Vector2.zero);
      8. if(hit.collider !=false&&
      9. hit.collider.name == gameObject.name &&
      10. gameObject.renderer.enabled ==true)
      11. {
      12. Debug.Log(gameObject.name);
      13. var cursorTexture =Resources.Load("cursorTextureMess")asTexture2D;
      14. Cursor.SetCursor(cursorTexture, hotspot, cursorMode);
      15. }
      16. else
      17. {
      18. Cursor.SetCursor(null, hotspot, cursorMode);
      19. }
      20. }
      21. }
    Thanks for any help in advance.
     
  2. RabenFutter

    RabenFutter

    Joined:
    Dec 30, 2012
    Posts:
    38
    you need this skript only once. just attach it to you camera or controler object and change this line:
    Code (CSharp):
    1. if(hit.collider !=false && hit.collider.name == gameObject.name && gameObject.renderer.enabled ==true)
    first off if the gameObject dident had an collider it would never get an hit so we dont need hit."collider !=false"

    you probaly dont want every gameObject which has an collider get the new curser so we need to change "hit.collider.name == gameObject.name" to maybe hit.tag == "object"
    make sure you add an tag with the strings "object" and mark your objects with it.

    in the end we have something like that
    Code (CSharp):
    1. if(hit.tag == "object" && hit.gameObject.renderer != null && hit.gameObject.renderer.enabled ==true){
     
  3. tytbone

    tytbone

    Joined:
    Jul 16, 2013
    Posts:
    18
    Thanks for trying to help but that didn't seem to work. I may just have to put aside this issue for now.

    EDIT: This seems to have worked for me, though, so thanks for setting me on the right track!

    if (hit.collider != null && hit.collider.CompareTag("CursorChange"))
     
  4. RabenFutter

    RabenFutter

    Joined:
    Dec 30, 2012
    Posts:
    38