Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Get the image component from the button clicked and create a new gameobject with that image

Discussion in 'Scripting' started by khrysller, Mar 15, 2021.

  1. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    Hello,

    I need some help on instantiation, any help on this will be great. This is the case that I have: I have a button with the image component on it so when I click on the button I would like to create a new object with the image from the button clicked and put this new gameobject as child of another gameobject.

    So this is the setup that I have:

    upload_2021-3-15_13-2-39.png

    So the pratical case is: When I click in PathA button it will create a new gameobject under SelectedGroup gameobject (doesn`t matter the name) with the image component and the image 01A on it.

    I have created a function for that but it is cloning everything on root of the scene:

    Code (CSharp):
    1.     public void PathFromA()
    2.     {
    3.         Instantiate(gameObject);
    4.     }
    Thanks for the help
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,033
    Images have a .sprite field.

    Steps to success:

    - get the
    Image
    off the object in question (GetComponent)

    - from the
    Image
    get the
    .sprite


    Now you have the
    Sprite
    . This may or may not be suitable depending on what you mean by,

    You may need to make a SpriteRenderer on a new GameObject and assign that Sprite to it.
     
  3. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    Thanks for the information. I think it is something like this (it is giving an error but I think this might be close it it)
    Code (CSharp):
    1.     public void PathFromA()
    2.     {
    3.         GameObject newObject = new GameObject("Image");
    4.         Instantiate(newObject.AddComponent<Image>().sprite(PathA.GetComponent<Image>().sprite));
    5.     }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,033
    Yeah, don't do lines like 4... first of all, break it all apart so you can reason about it step by step.

    You are already CREATING the new GameObject in line 3, so don't do any more "instantiating."

    - add a SpriteRenderer to this new GameObject (keep a reference to it).

    - get the Sprite out of the Image, assign it to a temporary variable.

    - assign the Sprite to the SpriteRender on the new GameObject.

    - move the new GameObject where you want.

    When you work like that, each single step can be reasoned about, you can use Debug.Log() to print out what is going on, and when one step fails (which it will!), you know how to track it down.

    One
    thing
    at
    a
    time.
    :)
     
  5. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    Thanks, Got all the stuff bellow and it is working.

    Code (CSharp):
    1.     public void PathFromA()
    2.     {
    3.         GameObject newObject = new GameObject("ImageX");
    4.         newObject.AddComponent<Image>();
    5.         newObject.GetComponent<Image>().sprite = PathA.GetComponent<Image>().sprite;
    6.  
    7.         GameObject SelectedGroup = GameObject.Find("SelectedGroup");
    8.         newObject.transform.parent = SelectedGroup.transform;
    9.     }
    Thank you for guiding me to find the solution. I really appreciated that.
     
    Last edited: Mar 15, 2021