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:
void instantiateGroupNumberBillboard()
{
billboardGroupNumber = Resources.Load("Prefabs/FloatingGroupPrefab") as GameObject;
Instantiate(billboardGroupNumber);
Debug.Log("Created billboard with ID: " + billboardGroupNumber.GetInstanceID());
}
public void updateBillboardGroups()
{
if(billboardGroupNumber == null)
{
instantiateGroupNumberBillboard();
}
BillboardGroupNumber billboard = billboardGroupNumber.GetComponent(typeof(BillboardGroupNumber)) as BillboardGroupNumber;
billboard.updateGroupNumber(5);
}
The BillboardGroupNumber script looks like this:
public class BillboardGroupNumber : MonoBehaviour
{
public int groupNumber = 0;
void Start()
{
Debug.Log("Started billboard with object ID: " + this.GetInstanceID());
}
public void updateGroupNumber(int newGroupNumber)
{
groupNumber = newGroupNumber;
}
}
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.
Resources.Load works only for assets located in a Resources directory. If you move your Prefab to something like Prefabs/Resources/FloatingGroupPrefab.prefab your code should work as expected.
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:
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:
BillboardGroupNumber billboard = (BillboardGroupNumber)FindObjectOfType(typeof(BillboardGroupNumber));
billboard.updateGroupNumbers(6);
But this does not:
BillboardGroupNumber billboard = groupNumberPrefab.GetComponent(typeof(BillboardGroupNumber)) as BillboardGroupNumber;
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.
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:
var floatingGroupPrefabAsset = Resources.Load("Prefabs/FloatingGroupPrefab") as GameObject;
var floatingGroup = Instantiate(floatingGroupPrefabAsset);
var billboardGroupNumber = floatingGroup.GetComponent<BillboardGroupNumber>();
billboardGroupNumber.updateGroupNumbers(6);