Search Unity

Change Player Sprite Based on Clicked Variable?

Discussion in 'Visual Scripting' started by fordani1994, Jun 28, 2021.

  1. fordani1994

    fordani1994

    Joined:
    Dec 4, 2019
    Posts:
    2
    So I have a Script that sets a scene variable based on which object has been clicked on,
    What I'm now trying to do is change the players sprite to that object when it has been clicked on.
    If anyone can help that would be much appreciated
     

    Attached Files:

  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,079
    Also hold the player in a Scene variable. Then get the picked objects SpriteRenderer.sprite(get) and set the player's sprite with SpriteRenderer.sprite(set).

    You can either manually add Player to Scene variables in edit mode or you can add a graph on Player GameObject that sets itself in a Scene variable in Start.
     
  3. fordani1994

    fordani1994

    Joined:
    Dec 4, 2019
    Posts:
    2
    So Ive set my Player(Alien) as a scene variable, and with this script, the player sprite does change, but only when the game button isnt playing. Once I click play it goes back to his default.

    Also the Picked Objects Get sprite is saying missing target?
     

    Attached Files:

    • help.png
      help.png
      File size:
      140.6 KB
      Views:
      293
  4. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,079
    Variables are not persistent between play sessions unless you're using Saved variables. And the missing target error is likely caused by the Picked Object variable having a null value when starting the game. The sprite switcher logic runs from the very start of the game every frame in Update even when you haven't picked something yet. So you either trigger the logic only when you pick something via UnityVS Custom Event. Or you add a Null Check node that checks if Picked Object has a value before continuing with setting the sprite.
     
    Last edited: Jun 29, 2021