简体   繁体   English

Unity 实例化的游戏对象抛出错误并表现得像一个预制件

[英]Unity instantiated gameobject throws an error and acts like a prefab

I'm trying to make a scrolling inventory system in unity using button UI elements.我正在尝试使用按钮 UI 元素统一制作滚动库存系统。 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.我还没有尝试联系制作本教程的人,因为此时它已经超过 2 年了,并且该频道至少有 7 个月没有活跃过。 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.预期结果:该按钮应成为 ListContent 的子项,在检查器中将其设置为inventoryTabWeapons。

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.此代码未使用预制件,唯一更改其父级的资产是模板的克隆,因此此错误对我来说毫无意义。

Make SURE that itemTemplatePrefab is a PREFAB, and make SURE that inventoryTabWeapons is NOT a prefab, but an actual gameObject in your scene. 确保itemTemplatePrefab是PREFAB,并确保inventoryTabWeapons不是预制件,而是场景中的实际gameObject。 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. 因此itemTemplatePrefab是预制件,而inventoryTabWeapons不是预制件。

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. 我想我问题的解决方案就是等待。

I have no idea why this is happening and it's not easy to find a good thread online, but I have found that caching the transform on object start seems to prevent it from get it destroyed (either by the Garbage Collector or Unity).我不知道为什么会发生这种情况,并且在网上找到一个好的线程并不容易,但是我发现在对象启动时缓存转换似乎可以防止它被销毁(由垃圾收集器或 Unity)。

So in your case I would re-write it as this:因此,在您的情况下,我会将其重写为:

[SerializeField]
private GameObject itemTemplatePrefab, inventoryTabWeapons;
// rename as pleased.
private Transform _tranformCache

private void Start()
{
   _transformCache = inventoryTabWeapons.transform;
}

public void GenerateItem(string name)
{
    // .. previous code ..
    item.transform.SetParent(_transformCache, false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM