Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Accessing a property of a prefab component loaded from directory during runtime

Discussion in 'Prefabs' started by huub8, Aug 8, 2022.

  1. huub8

    huub8

    Joined:
    Dec 23, 2019
    Posts:
    5
    Hi all,

    I have an object in my scene that has a script attached to it that loads a prefab from my prefabs folder. This prefab has a "BillboardGroupNumber" script attached to it. I'm instantiating this prefab and attempting to update the groupNumber by doing this:


    Code (CSharp):
    1.    void instantiateGroupNumberBillboard()
    2.     {
    3.         billboardGroupNumber = Resources.Load("Prefabs/FloatingGroupPrefab") as GameObject;
    4.         Instantiate(billboardGroupNumber);
    5.         Debug.Log("Created billboard with ID: " + billboardGroupNumber.GetInstanceID());
    6.     }
    7.  
    8.     public void updateBillboardGroups()
    9.     {
    10.         if(billboardGroupNumber == null)
    11.         {
    12.             instantiateGroupNumberBillboard();
    13.         }
    14.      
    15.         BillboardGroupNumber billboard = billboardGroupNumber.GetComponent(typeof(BillboardGroupNumber)) as BillboardGroupNumber;
    16.         billboard.updateGroupNumber(5);
    17.      
    18.     }

    The BillboardGroupNumber script looks like this:

    Code (CSharp):
    1. public class BillboardGroupNumber : MonoBehaviour
    2. {
    3.     public int groupNumber = 0;
    4.  
    5.     void Start()
    6.     {
    7.         Debug.Log("Started billboard with object ID: " + this.GetInstanceID());
    8.     }
    9.  
    10.     public void updateGroupNumber(int newGroupNumber)
    11.     {
    12.         groupNumber = newGroupNumber;
    13.     }
    14. }

    I expected the groupNumber to change from the default of 0 to 5, but it still shows 0 in the inspector. I also don't see any other instantiations. How can it not be changed? Am I somehow calling it on another object?

    The 2 Instance ID's that I'm printing do show different numbers.
     
  2. huub8

    huub8

    Joined:
    Dec 23, 2019
    Posts:
    5
    Anybody?
     
  3. MirceaI

    MirceaI

    Unity Technologies

    Joined:
    Nov 24, 2020
    Posts:
    52
  4. huub8

    huub8

    Joined:
    Dec 23, 2019
    Posts:
    5
    I wasn't completely clear, but the Prefabs folder itself is already in the Resources folder. (and placing the prefab in the root of the Resources folder doesn't change anything for me. Also, I do manage to create the prefab, but I just can't seem to properly reference it.

    I have made a bit of progress myself, namely that printing the object ID from the Update function within the BillboardGroupNumber gives a different ID than printing the ID of the object that I get by doing this:

    Code (CSharp):
    1. BillboardGroupNumber billboard = groupNumberPrefab.GetComponent(typeof(BillboardGroupNumber)) as BillboardGroupNumber;
    So the line above doesn't work for me, the billboard reference that it produces has a positive object ID, while printing from the Update function within the actual GroupNumberBillboard gives me a different negative ID.

    If I instead try to refer to the GroupNumberBillboard that has been instantiated by using Find then it does work, and I can change the value.

    So this works:
    Code (CSharp):
    1. BillboardGroupNumber billboard = (BillboardGroupNumber)FindObjectOfType(typeof(BillboardGroupNumber));
    2.         billboard.updateGroupNumbers(6);
    But this does not:
    Code (CSharp):
    1. BillboardGroupNumber billboard = groupNumberPrefab.GetComponent(typeof(BillboardGroupNumber)) as BillboardGroupNumber;
    2.         billboard.updateGroupNumbers(5);
    Using find is not ideal, since I will be having multiple BillboardGroupNumber objects, and I still don't understand what I'm doing wrong in the first place.
     
  5. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    222
    Your problem is that you are querying the component from the asset object and not the created clone. So something like this should fix your problem:


    Code (CSharp):
    1. var floatingGroupPrefabAsset = Resources.Load("Prefabs/FloatingGroupPrefab") as GameObject;
    2. var floatingGroup = Instantiate(floatingGroupPrefabAsset);
    3. var billboardGroupNumber = floatingGroup.GetComponent<BillboardGroupNumber>();
    4. billboardGroupNumber.updateGroupNumbers(6);
     
  6. huub8

    huub8

    Joined:
    Dec 23, 2019
    Posts:
    5
    Ah yes! That is exactly what I was doing wrong. Thank you!