Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

[Unity2d Sprite Rendering Question] Grabbing and altering the sprite renderer of another game object

Discussion in '2D' started by Sabrekitty, Dec 2, 2016.

  1. Sabrekitty

    Sabrekitty

    Joined:
    Dec 2, 2016
    Posts:
    3
    Hello all!

    I'm new to coding and unity,

    I dislike the unity UI system as it makes simple games massive.
    I've created buttons with colliders and raycasted them to get the positions.

    The desired outcome: clicking on my buttons and changing the sprite of another object (walking up sprite/down sprite).

    The problem: I can get one sprite to change in my array, but the moment I set up multiple sprites in my code, the sprite renderer stops swapping them out.
    Code (csharp):
    1.  
    2. // This is my main game object I attach to the button manager
    3.     public SpriteRenderer spriteStack;
    4. // The array  
    5.     public Sprite[] headButtonSprites;
    6.  
    7. // Grabbing colliders to raycast
    8. // This works on its own!
    9.    if (hit.collider.gameObject.tag == "up")
    10.             {
    11.                 spriteStack.sprite = headButtonSprites[0];
    12.                 PlayerBehaviour.dir = Vector2.up;
    13.             }
    14. // The player still moves but the sprites stop swapping as soon as I add more to the array.
    15.             else if (hit.collider.gameObject.tag == "down")
    16.             {
    17.                 spriteStack.sprite = headButtonSprites[1];
    18.                 PlayerBehaviour.dir = -Vector2.up;
    19.             }
    20.  
     
    Last edited: Dec 2, 2016
  2. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    What do you mean by multiple sprites in the code? you seem to have at least 2 in that sample...
    and what do you mean by stops swapping them out?
     
  3. Sabrekitty

    Sabrekitty

    Joined:
    Dec 2, 2016
    Posts:
    3
    Yes, I only wrote two because I didn't think it nessecary to write the entire logic out when the pattern is clear from my code.
    I'm not sure what was intended by the ellipses but it doesn't come across well in a help thread.

    I solved the problem out of interest sake.

    When you are Raycasting a 2DCollider simply check for null.

    Here's the whole code of my button manager for those who might try the same thing:
    Code (csharp):
    1.  
    2. {
    3.  
    4.     // This is useful if you are creating a small game with UI buttons
    5.     //Unity's Canvas System is massive
    6.     //Create A public SpriteRenderer, link your player object to it (The sprite you want to change)
    7.     //Create a sprite array for the buttons to
    8.    
    9. public SpriteRenderer spriteStack;
    10.     public Sprite[] headButtonSprites;
    11.  
    12.     void Update()
    13.     {
    14.         if (Input.GetMouseButtonDown(0))
    15.         {
    16.   // Create a 2DCollider around your buttons
    17. // This will detect when the box collider based on the mouse position
    18.             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    19. // Check for null, tag each button appropriately relate them to your main player/object script.
    20.             if (hit.collider != null)
    21.             {
    22.                 if (hit.collider.gameObject.tag == "up")
    23.                 {
    24.                     spriteStack.sprite = headButtonSprites[0];
    25.                     PlayerBehaviour.dir = Vector2.up;
    26.                 }
    27.                 else if (hit.collider.gameObject.tag == "down")
    28.                 {
    29.                     spriteStack.sprite = headButtonSprites[1];
    30.                     PlayerBehaviour.dir = -Vector2.up;
    31.                 }
    32.                 else if (hit.collider.gameObject.tag == "left")
    33.                 {
    34.                     spriteStack.sprite = headButtonSprites[2];
    35.                     PlayerBehaviour.dir = -Vector2.right;
    36.                 }
    37.                 else if (hit.collider.gameObject.tag == "right")
    38.                 {
    39.                     spriteStack.sprite = headButtonSprites[3];
    40.                     PlayerBehaviour.dir = Vector2.right;
    41.                 }
    42.             }
    43.         }
    44.     }
    45. }
    46.  
    47.  
    Newbie mistake:)
     
  4. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Why reinvent the wheel, I agree somethings are just plain stupid, but that's what we have to work with, I would love to see that work on different screen resolutions, the canvas is made for that type of thing, and you might find that swapping in sprites like that the GC might look interesting.
     
  5. Sabrekitty

    Sabrekitty

    Joined:
    Dec 2, 2016
    Posts:
    3
    So funny story, if you attach things to the camera the resolutions USUALLY scale pretty well.

    Its not too much of a headache because we are using mostly one resolution for these games anyway.

    So out of interest, my code works! Except.. I wanted it to work for touch devices, onmousedown(0) is meant to do that.
    But the raycasting doesn't seem to work for mobile devices.

    So I just went back to UI.

    No harm in trying new coding I guess.
     
  6. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    Raycasting works fine with touch devices. In touch devices though there's no mouse to get a position from. Assuming you have a touch, your raycast should look something like
    Code (CSharp):
    1. if (Input.touchCount > 0) {
    2.    RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.touches[0].position), Vector2.zero));