You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity3D中实例化的Sphere预制体无法移动问题求助

实例化Sphere无法移动的问题修复

核心问题

你当前代码操作的是加载的预制体资源本身,而非场景中实例化出来的对象。Resources.Load获取的是项目里的预制体文件,Instantiate会基于这个预制体生成场景中的实例,但你没有将实例的引用保存到sphereInstantiated变量中,反而一直修改预制体的位置,所以场景里的球不会有任何移动。

修正代码(Resources.Load方式)

using UnityEngine;

public class SphereController : MonoBehaviour
{
    private GameObject sphereInstantiated;

    void Start()
    {
        // 先加载预制体资源
        GameObject spherePrefab = Resources.Load<GameObject>("Character");
        // 实例化并将场景中的实例引用赋值给变量
        sphereInstantiated = Instantiate(spherePrefab, new Vector3(0, 0, 0), Quaternion.identity);
    }

    void Update()
    {
        // 现在修改的是场景中实例的位置
        sphereInstantiated.transform.position += Vector3.forward * 100 * Time.deltaTime;
    }
}

修正代码(Public变量挂载方式)

如果用public变量挂载预制体,同样需要保存实例化后的引用:

using UnityEngine;

public class SphereController : MonoBehaviour
{
    public GameObject spherePrefab; // 在Inspector面板中拖入你的Sphere预制体
    private GameObject sphereInstantiated;

    void Start()
    {
        // 实例化并保存场景中的实例引用
        sphereInstantiated = Instantiate(spherePrefab, new Vector3(0, 0, 0), Quaternion.identity);
    }

    void Update()
    {
        sphereInstantiated.transform.position += Vector3.forward * 100 * Time.deltaTime;
    }
}

内容的提问来源于stack exchange,提问作者user20324932

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 10:10:32