Search Unity

i wanna code something in a 2d sprite pls help

Discussion in '2D' started by marcxstt, Oct 21, 2022.

  1. marcxstt

    marcxstt

    Joined:
    Oct 21, 2022
    Posts:
    2
    upload_2022-10-21_0-41-25.png
    these red squares are my 2d sprites that i wanna code, basically i want to make them visible or invisible using a switch case, but to do this i need to add a c# file to the sprite to be able to reference it in my main c# file, can i get some help
     

    Attached Files:

  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    From the way you worded your question, it looks like you're asking how to do the above. The answer is that you drag the second script from your project window onto the object the exact same way you added the first script.

    If your question is something else, please clarify.

    If all you want to do is toggle the visibility of the sprites, an if statement is a lot simpler to do than a switch statement. Look up some tutorials on youtube to learn more.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I'm struggling to understand what you need... this can be a useful template to help in technical communications:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above.
     
  4. marcxstt

    marcxstt

    Joined:
    Oct 21, 2022
    Posts:
    2
    upload_2022-10-21_15-12-23.png
    upload_2022-10-21_15-12-35.png
    basically what I want is in the update, you can see there is an empty if statement, I want to make it so when the condition of that if statement happens (i.e. when i press f) the red cubes go invisible
     
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,464
    Set the DieCube's alpha to 0 when you press F.
     
  6. NeilB133

    NeilB133

    Joined:
    May 30, 2022
    Posts:
    210
    If you make a prefab of the red square thing, pass it into the script like

    public GameObject redSquare;

    Code (CSharp):
    1. if (Input.GetKeyDown("F"))  {
    2.  
    3.   redSquare.SetActive(false);
    4.  
    5. }