Hello
I want to attach a script to an instantitated prefab in my scene and I’m using below code to do that.
I can successfully add script to instantiated prefab but I cannot change the variables in that script.
Even if I try that , when I print the values of the variables in my script, they all seems like their default values
Do you know how can I fix that?
GameObject prefabGO = prefabObj.gameObject;
if (prefabGO != null) {
Debug.Log("[SceneCreator] Creating MeshTextureDrawer ITEMID:"+itemID+" ModelType:"+modelType+" SupplierID:"+supplierID+" StoreOwner:"+storeOwner);
MeshTextureDrawer mtd = prefabGO.AddComponent<MeshTextureDrawer>();
mtd.init();
if (mtd != null) {
mtd.itemID = itemID;
mtd.supplierID = supplierID;
mtd.modelType = modelType;
mtd.storeOwnerName = storeOwner;
mtd.thisObj = prefabObj;
} else {
Debug.Log("[SceneCreator] TextureDrawer Script is Null");
}
}
Thanks
Tolga
tolgatanriverdi:
Hello
I want to attach a script to an instantitated prefab in my scene and I’m using below code to do that.
I can successfully add script to instantiated prefab but I cannot change the variables in that script.
Even if I try that , when I print the values of the variables in my script, they all seems like their default values
Do you know how can I fix that?
GameObject prefabGO = prefabObj.gameObject;
if (prefabGO != null) {
Debug.Log("[SceneCreator] Creating MeshTextureDrawer ITEMID:"+itemID+" ModelType:"+modelType+" SupplierID:"+supplierID+" StoreOwner:"+storeOwner);
MeshTextureDrawer mtd = prefabGO.AddComponent<MeshTextureDrawer>();
mtd.init();
if (mtd != null) {
mtd.itemID = itemID;
mtd.supplierID = supplierID;
mtd.modelType = modelType;
mtd.storeOwnerName = storeOwner;
mtd.thisObj = prefabObj;
} else {
Debug.Log("[SceneCreator] TextureDrawer Script is Null");
}
}
Thanks
Tolga
Yeah I have had this problem before, for me it seemed to work better when I used a setter method instead trying to assign the variables directly…
i.e. something like prefabGo.GetComponent ().setItemID(itemID);
void setItemID (int itemID#) {
itemID = itemID#;
}