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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Editing child of prefab after instantiation without using .find?

Discussion in 'Prefabs' started by alphaenix, Apr 24, 2023.

  1. alphaenix

    alphaenix

    Joined:
    Apr 4, 2023
    Posts:
    2
    Hi! I'm working on a medical VR project and am using scriptable objects alongside prefabs to fill out patient information in a chart. All of the information "boxes" / list items are UI prefabs that I need to fill out with scriptable object information. Every solution I have found is to use the ".find" function, but in VR that's a LOT of draw calls, especially for every single aspect of the patient. Is there ANY other way to go about this? Right now I'm getting errors because I'm not editing the instance of the prefab. I've attached an image of my prefab, where it spawns, and the code for this specific Population method. If any more information is needed to get help, let me know :)

    private void PopulateAllergies(SO_Allergy[] allergyList)
    {
    foreach (SO_Allergy reference in allergyList)
    {
    GameObject allergyItem = Instantiate(lI2Text1Icon, pAllergyList.transform);

    allergyItem.transform.GetChild(2).GetComponent<TextMeshPro>().text = reference.allergyName;
    allergyItem.transform.GetChild(3).GetComponent<TextMeshPro>().text = reference.allergyReaction;

    GameObject allergyMarker = Instantiate(severityMarker);
    allergyMarker.transform.parent = allergyItem.transform.GetChild(1);

    allergyItem.transform.localScale = new Vector3(1, 1, 1);
    allergyItem.transform.localPosition = new Vector3(0, 0, 0);

    allergyMarker.transform.localScale = new Vector3(1, 1, 1);
    allergyMarker.transform.localPosition = new Vector3(0, 0, 0);

    allergyMarker.transform.GetChild(3).GetComponent<TextMeshPro>().text = ConvertSeverityString(reference.allergySeverity);
    allergyMarker.transform.GetChild(3).GetComponent<TextMeshPro>().color = ConvertSeverityColor(reference.allergySeverity);
    }
    }

    The necessary information should be editing the TMP components of the Text objects and also spawning a severity marker underneath the second GameObject.


    The prefab itself should be spawning underneath the allergies list (which it currently is), but it spawns without any information changed and only spawns one instead of the amount I'm giving it.
     
  2. alphaenix

    alphaenix

    Joined:
    Apr 4, 2023
    Posts:
    2
    Okay I figured out my problem, after HOURS of attempts it turns out I was using the wrong TextMeshPro thing, needed to use TextMeshProUGUI and that solved basically all my problems.