Unity库存物品对象定位异常:对象被设置至画布外
库存物品定位错误,被置于画布外
我在实现用户库存展示功能,需求是仅显示用户拥有的物品。目前物品对象可正常激活,且能按用户获取顺序排列,但定位异常,被设置到了画布外面。
已做排查:
- 无报错信息输出
- 设置
position数组前,已核对场景中的位置并填入正确数值,但对象仍处于错误位置 - 添加Debug日志排查,日志显示的是预设的目标位置,但实际显示位置不符
相关截图
代码实现
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
相关产品推荐
相关产品推荐






