Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing source image of an object

Discussion in 'Scripting' started by FalconJ, Mar 30, 2015.

  1. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    I need to change the source image of an object. I already tried using mainTexture like this :
    Code (CSharp):
    1.             GameObject go = (GameObject)Instantiate (selectCharacter.Instance.availChar [randomizer]);
    2.             img = go.GetComponent<Image>();
    3.             img.enabled = true;
    4.             img.mainTexture = Resources.Load<Texture2D>(imgPath[1]);
    But I can't run it because there's an error :
    Any idea how?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
  3. ShabihDesperado

    ShabihDesperado

    Joined:
    Oct 27, 2013
    Posts:
    41
    did you try with image.sprite?
     
  4. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    I don't have sprite on the object because it's a button. The image comes from "Image (Script)" as attached below.
     

    Attached Files:

  5. N3ms

    N3ms

    Joined:
    Oct 24, 2013
    Posts:
    27
  6. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    So the button need to be applied with sprite.create? How to know if the sprite's image will fit with the button's image?