Hello there,
So, I’m making a 2D puzzle platformer game in Unity3D, and I wanted to instantiate an object at the current mouse position. The player would move the mouse to the desired position and then press and hold a button -in my case it’s the ‘C’ button- to instantiate the object. Here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RainAdd : MonoBehaviour
{
public GameObject cloud;
Vector3 mousePos;
int x = 1;
// Update is called once per frame
void Update()
{
mousePos = Input.mousePosition;
if (Input.GetKey(KeyCode.C))
{
while (x != 0)
{
Instantiate(cloud, mousePos, Quaternion.identity);
x = 0;
}
}
if (Input.GetKeyUp(KeyCode.C))
{
Destroy(GameObject.Find("Cloud(Clone)"));
x = 1;
}
}
}
The problem is this: The object does get instantiated but it’s not visible neither in the scene view nor in the game view, and I have tested this code with various other positions, and every time it works fine, except with the mouse position. Any idea why?
Thanks in advance!