简体   繁体   English

unity Object 池对象不可见

[英]Unity Object Pool Objects Invisible

The following code below shows what I made.下面的代码显示了我所做的。 (Pooled square is a game object I set up in another c# file) (Pooled square是我在另一个c#文件中设置的游戏object)

[SerializeField] private Camera mainCamera;
private List<GameObject> PooledSquares = new List<GameObject>();
private Camera cam;

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Mouse0)) {

        Vector3 MousePosition = mainCamera.ScreenToWorldPoint(Input.mousePosition);
        GameObject pooledSquare =  ObjectPool.SharedInstance.GetPooledObject();

        if (pooledSquare != null)
        {
            pooledSquare.SetActive(true);
            pooledSquare.transform.position = MousePosition;
        }
    }
}

There are no syntax errors or node errors, and all the physics work such as Rigidbody, the objects are just not visible.没有语法错误或节点错误,所有的物理工作,如刚体,对象只是不可见。 Then I realized I didn't have a prefab.然后我意识到我没有预制件。 So I made a prefab.所以我做了一个预制件。 Still the same issue.还是一样的问题。 If you need any more information contact me.如果您需要更多信息,请与我联系。

Thanks for your help.谢谢你的帮助。

var mousePosition = Input.mousePosition;
mousePosition.z = 5f; // world position from the camera.

You should give az position otherwise it is 0 which means the object is put at the position of the camera.你应该给 az position 否则它是 0 这意味着 object 被放在相机的 position。

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

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