The following code below shows what I made. (Pooled square is a game object I set up in another c# file)
[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.
CodePudding user response:
var mousePosition = Input.mousePosition;
mousePosition.z = 5f; // world position from the camera.
You should give a z position otherwise it is 0 which means the object is put at the position of the camera.