0
votes

I'm trying to make a scrolling inventory system in unity using button UI elements. I made a template for a button which is not a prefab and instantiated a game object with that template, which should make a clone of the template. Instead of that it acts like a prefab and gives me an error.

I happened to find a tutorial that worked well enough. I followed this tutorial up to the point where I had to set the parent of the instantiated gameObject. The tutor did this without trouble. I have not tried contacting the person who made this tutorial as it is more than 2 years old at this point and the channel hasn't been active for at least 7 months. I tried changing his code and removing the button prefab. I found a similar problem on this site, but the solution didn't work for me.

[SerializeField]
private GameObject itemTemplate, inventoryTabWeapons;

private GameObject item;

public void GenerateItem(string name)
{
   item = Instantiate(itemTemplate) as GameObject;
   item.SetActive(true);
   item.transform.SetParent(inventoryTabWeapons.transform, false); //This line is where the error brings me to.
}

I would like to post screenshots of the results here, but I apparently don't have enough reputation points.

expected result: The button should be made a child to ListContent, which is set as inventoryTabWeapons in the inspector.

actual result: The button isn't a child of anything.

I get the following error:

Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption

No prefabs were used for this code and the only asset that has its parent changed is a clone of a template, so this error makes no sense to me.

2
The Instantiate function only applies to prefabs. You can't instantiate something that isn't a prefab.Sean Carey
@SeanCarey When using [SerializeField] you can drag and drop any gameobject via the inspector and instantiate using that gameobject. The same error applies when the button is a prefab and I drag and drop the prefab via the inspector.sighet someone
No, you're wrong. You can not instantiate something that is not a prefab. That's why your code doesn't work.Sean Carey
@SeanCarey As soon as I saw your first comment, I tried turning the button into a prefab and dragging said prefab in the inspector to the correct component. Absolutely nothing changed. There have been several instances in the same project where I did use this method without turning the gameobjects into prefabs and it worked. Following you comment just now, I tried it again and it worked without fail. I'm afraid this is not the solution to my problem.sighet someone

2 Answers

0
votes

Make SURE that itemTemplatePrefab is a PREFAB, and make SURE that inventoryTabWeapons is NOT a prefab, but an actual gameObject in your scene. Then re-write your code to the following:

[SerializeField]
private GameObject itemTemplatePrefab, inventoryTabWeapons;

public void GenerateItem(string name)
{
    var item = Instantiate(itemTemplatePrefab);
    item.SetActive(true);
    item.transform.SetParent(inventoryTabWeapons.transform, false);
}

So itemTemplatePrefab IS a prefab, and inventoryTabWeapons is NOT a prefab.

0
votes

The error suddenly disappeared when I exited the editor last night and reopened it this morning, even though I already tried exiting and reopening the editor. I got several different errors in its stead which I managed to resolve. I guess the solution to my problem was just waiting.