Search Unity

Resolved How to make a sprite disappear and reappear from a script?

Discussion in 'Getting Started' started by Ryan1Cooper, May 3, 2023.

  1. Ryan1Cooper

    Ryan1Cooper

    Joined:
    Apr 20, 2023
    Posts:
    7
    In my game I use rocket engines and I have a sprite for their flames. I need a flame to appear when I press the key that starts an engine and to disappear when I unpress this key. I tried using material for this but Unity sais that color of the material is not a variable and cannot be modified. So how do I do it?
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,437
    You can make your own materials, and those are okay to change at runtime.

    Or you can get a reference to the object that represents the flames and set it inactive/active at runtime.

    Or you can get a reference to the component that describes the flames (the Renderer on a 3D object or the Image or something on a 2D object), and set it enabled/disabled at runtime.
     
  3. Ryan1Cooper

    Ryan1Cooper

    Joined:
    Apr 20, 2023
    Posts:
    7
    Thank you very much that set active advise really helped! But talking about transparency I did try to change it at runtime using my own materials before asking the question and it didn't work. "Color of the material is not a variable and cannot be modified" is referred to that. So what's the catch here? Do I need to set some flag "allow to change color" active in the material file?
     
  4. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    It tells you what's wrong in the error message, you need to set the colour of the flame as a variable.
     
  5. Ryan1Cooper

    Ryan1Cooper

    Joined:
    Apr 20, 2023
    Posts:
    7
    So how do I do that?
     
  6. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    I'm not sure.