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

Instantiated duplicate of game object but duplicated object is not showing

Discussion in 'Scripting' started by Naomikho, Apr 7, 2022.

  1. Naomikho

    Naomikho

    Joined:
    Jan 25, 2021
    Posts:
    3
    Hello, I am new to Unity, and I am currently making a messaging system that looks like this.
    1.png
    Following this design, I want to be able to create more speech bubbles like this as the conversation moves on. However, although I already used Instantiate to duplicate this game object, the duplicated game object is not showing up at all in the scene when I'm playing. I am able to manipulate it's components and children however, so it means that the object was definitely instantiated successfully. The code snippet below is how I duplicate the game object and manipulate its components and children. Am I missing something or did I do something wrongly? As a side note this project is 3D but all these are UI elements so they are 2D and have no zpos.
    Code (CSharp):
    1.                         lastMsgYPos -= 230.0f;
    2.                         GameObject duplicate = GameObject.Instantiate(playerMessage);
    3.                         duplicate.gameObject.SetActive(true);
    4.                         //change ypos here
    5.                         duplicate.gameObject.GetComponent<RectTransform>();
    6.                         var pos = duplicate.gameObject.GetComponent<RectTransform>().localPosition;
    7.                         duplicate.gameObject.GetComponent<RectTransform>().localPosition = new Vector3(pos.x, lastMsgYPos, 0);
    8.                         pos = duplicate.gameObject.GetComponent<RectTransform>().localPosition;
    9.                         //duplicate.gameObject.transform.GetChild(0).text = ; add every line later change to player name
    10.                         duplicate.gameObject.transform.GetChild(0).gameObject.GetComponent<Text>().text = currBlock.getActor()[currentLineIndex];
    11.                         duplicate.gameObject.transform.GetChild(2).GetChild(0).gameObject.GetComponent<Text>().text = currBlock.getText()[currentLineIndex];
    12.                         duplicate.gameObject.transform.GetChild(2).gameObject.SetActive(false);
    13.                         duplicate.gameObject.transform.GetChild(1).gameObject.SetActive(true);
    14.                         //player image
    15.                         StartCoroutine(handleMessageAnimation(playerMessage));
    Here is a reference of my game object and its hierarchy. In this case I am trying to duplicate playerMessage (personMessage is similar but is just positioned differently).
    2.PNG
     
  2. Naomikho

    Naomikho

    Joined:
    Jan 25, 2021
    Posts:
    3
    upload_2022-4-7_14-8-1.png
    Apparently the object was instantiated in the root hierarchy, so it's because I didn't set it's parent. I have managed to fixed it although the UI looks weird now; probably just have to fix the position or something. Please do tell if you have any suggestions on the best way to fix the positions.
    Edit: Problem solved, sorry for the trouble.
    upload_2022-4-7_14-15-46.png
     

    Attached Files:

    Last edited: Apr 7, 2022
  3. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    hello, glad you figured out your issues !

    One quick advice to help you get started, i strongly recommend you create variables to shorten your lines and improve readability, for example:

    Code (CSharp):
    1. duplicate.gameObject.transform.GetChild(2).GetChild(0).gameObject.GetComponent<Text>().text = currBlock.getText()[currentLineIndex];
    2. duplicate.gameObject.transform.GetChild(2).gameObject.SetActive(false);
    could be:

    Code (CSharp):
    1. var labelRoot = duplicate.gameObject.transform.GetChild(2);
    2. var label = labelRoot.GetChild(0).gameObject.GetComponent<Text>();
    3.  
    4. label.text = currBlock.getText()[currentLineIndex];
    5. labelRoot.gameObject.SetActive(false);
    Readability is one aspect but main and most important idea is that through naming you add meaning to your code.

    Imagine in 6 month you or someone else that joined your project adds a child gameobject in that hierarchy, suddenly it no longer works because GetChild(2) doesn't correspond to the same object. You might not remember what you wanted out of it exactly, but if you have created named variables the original intent can be clear.

    And perhaps "labelRoot" is still overly generic, maybe it can be something more specific like "userNameLabelRoot", or "messageLabelRoot" etc.
     
  4. Naomikho

    Naomikho

    Joined:
    Jan 25, 2021
    Posts:
    3
    Thank you for your suggestion! My friend tole me to refactor too; I wasn't thinking about it because I was busy cracking my head over how to make things work haha. I'll definitely have to refactor my code soon. :(