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

Unity库存物品对象定位异常:对象被设置至画布外

库存物品定位错误,被置于画布外

我在实现用户库存展示功能,需求是仅显示用户拥有的物品。目前物品对象可正常激活,且能按用户获取顺序排列,但定位异常,被设置到了画布外面。

已做排查:

  • 无报错信息输出
  • 设置position数组前,已核对场景中的位置并填入正确数值,但对象仍处于错误位置
  • 添加Debug日志排查,日志显示的是预设的目标位置,但实际显示位置不符

相关截图

  • 对象位置示意图
  • 调试日志截图
  • 苹果物品Inspector面板
  • 双倍速度物品Inspector面板
  • 超级力量物品Inspector面板

代码实现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class Filters : MonoBehaviour
{
    [Header("Items")]
    public GameObject doubleSpeed;
    public GameObject doubleJump;
    public GameObject superForce;
    public GameObject noGravity;
    public GameObject noHit;
    public GameObject apple;
    private GameObject[] items;
    public int index = 0;
    public float[] position = new float[6] { -125.8f, -78.6f, -24.1f, 23.1f, 80.69913f, 36.3375f };
    private float x;

    // Update is called once per frame
    void Update()
    {
        Player player = FindObjectOfType<Player>();
        Filter(doubleJump, player.doubleJumps);
        Filter(doubleSpeed, player.doubleSpeeds);
        Filter(superForce, player.superForces);
        Filter(noGravity, player.noGravitys);
        Filter(noHit, player.noHits);
        Filter(apple, player.apples);
        items = GameObject.FindGameObjectsWithTag("Have");
        if(items.Length != 0)
        {
            for(int i = 0; i < items.Length; i++)
            {
                items[i].transform.position = new Vector3(position[i], 52.8f , 1f);
                items[i].SetActive(true);
                Debug.Log("Changed the position and set active new position:" + items[i].transform.position);
            }
        }
    }
    void Filter(GameObject item, int quantity)
    {
        if(quantity < 1)
        {
            item.SetActive(false);
            Debug.Log("less than 1");
        }
        else
        {
            item.SetActive(true);
            Debug.Log("set active");
            item.tag = "Have";
            Debug.Log("set the tag");
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 07:35:19