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

Question How can I change the cube's sprite through the script?

Discussion in 'Scripting' started by LazyGhost15, Jul 23, 2023.

  1. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    If ill drag and drop the sprite into my cube it will change to that sprite automatically but how do I do the same like when I drag and drop the sprite but through script?

    I need the cube to change its sprites several times in my game.
     
  2. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    109
    Code (CSharp):
    1. yourPlayer.GetComponent<SpriteRenderer>().sprite = newSprite;
     
  3. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    Cubes and 3D shapes have SpriteRenderer?
     
  4. LazyGhost15

    LazyGhost15

    Joined:
    Feb 12, 2022
    Posts:
    88
    I figured out what I need to do but I dont know how to access it, I need to change the Base map in the material, but idk how to do it.

    upload_2023-7-23_11-5-51.jpeg
     

    Attached Files:

  5. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    109
    Sorry, I thought you are talking about 2D

    If you want the change the base map you'll want to modify the "mainTexture" -> material.mainTexture
    and assign new Texture to it.

    If you want to change the material you will access it through GetComponenter<Renderer>().material = newMaterial;
     
    LazyGhost15 likes this.