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. Dismiss Notice

Prefab somehow doesnt use given Sprite

Discussion in '2D' started by Fe0dor, Sep 7, 2020.

  1. Fe0dor

    Fe0dor

    Joined:
    Jan 18, 2020
    Posts:
    17
    Hi,

    So I am making a object shooting little bullets and for that I made a prefab for the bullet.
    Based on a Value the bullet should change its Sprite and because its a prefab I cant attach them manually so I had to use Resources.Load.

    So I made a Resources folder in the assets folder and put only the given Sprite "Bullet_Normal" in it but it still doesnt work. What am I diong wrong? And how could I fix this?
    Also all the other functions with Rigidbodys a.s.o. all work perfectly and only the sprite is missing when running the game.
    Thanks in advance

    Code (CSharp):
    1. public class Bullet : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     Sprite spriteB;
    5.     Rigidbody2D rig;
    6.     float moveSpeed;
    7.     GameObject player;
    8.     GameObject gunPoint;
    9.  
    10.  
    11.    
    12.     void Start()
    13.     {
    14.         //
    15.         SetBulletType(0);
    16.         //
    17.  
    18.         rig = gameObject.GetComponent<Rigidbody2D>();
    19.         gunPoint = GameObject.Find("Gunpoint");
    20.         gameObject.transform.position = gunPoint.transform.position;
    21.         player = GameObject.Find("Player");
    22.        
    23.         moveSpeed = 100f;
    24.         SetVelocity();
    25.         gameObject.GetComponent<SpriteRenderer>().sprite = spriteB;
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.      
    32.     }
    33.  
    34.     void SetVelocity ()
    35.     {
    36.            rig.velocity = player.GetComponent<Rigidbody2D>().transform.up * moveSpeed * Time.deltaTime;
    37.     }
    38.  
    39.     public void SetBulletType(int type)
    40.     {
    41.        switch(type)
    42.         {
    43.             case 0:
    44.                 Debug.Log("BulletType: Normal");
    45.                 spriteB = Resources.Load("Bullet_Normal", typeof(Sprite)) as Sprite;
    46.                 break;
    47.  
    48.             default:
    49.                 Debug.LogError("BulletType not found");
    50.                 break;
    51.         }
    52.     }
    53. }
    its a prefab I couldnt attach a sprite manually because
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    I don't know of any reason a prefab should prevent you from attaching the sprite directly, so going to need to know what issue you're running into there. Also, what behavior are you getting here?
     
  3. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    You need to assign the Sprite to its SpriteRenderer. What you've done is get a reference to the sprite. It has yet been told to render anywhere. On the next line after line 45 try this (it'll only work if your bullet has a SpriteRenderer).

    this.GetComponent<SpriteRenderer>().sprite = spriteB;
     
    Fe0dor likes this.
  4. Fe0dor

    Fe0dor

    Joined:
    Jan 18, 2020
    Posts:
    17
    This worked! Thank you