- Home /
I cannot see my cloned prefabs.
Hello!!
I'm trying to clone one of my prefabs (capsule named X) when I press down my left button mouse. My capsule is cloned each time I press the button and I know this because I can see X(cloned) in the hierarchy, but there aren't any capsule in the "map".
Here Is my code:
var X : Transform;
function Start () {
}
function Update(){
Debug.Log(Input.GetMouseButtonDown(0));
if (Input.GetMouseButtonDown(0) == true){
Instantiate(X,Vector3 (Input.mousePosition.x,0,Input.mousePosition.z),
Quaternion.identity); //Creating clones
Debug.Log(Input.mousePosition);
}
}
How can I see each capsule? I've read the unity tutorials of instantiate but I don't know if I need to add anything else.
thank you for your answers^^
Input.mousePosition
is a screen position, which ranges from 0, 0
all the way to length and height (in pixels) of your screen.
For instance, if the mouse is in the middle of my 1080p screen, then the mouse position will be: 960, 540
, which is very far away from the origin.
Are you sure you want to instantiate objects using screen positions?
I'm a bit noob (sure you realise that xD) so... do you have any other idea to instantiate an object in te position of my mouse (when I press left button)? In fact I'm trying to do that and I thought that was the easier way.
Thats a different question and has already been answered many times before. There are many tutorials out there that can help you.
You could tie the Instantiate script into an object parenting the camera, and use the camera's look position to call the instantiate; but that would depend on what type of camera you were using; First-Person, Third-Person, Isometric. You could also Draw a Raycast from the camera or another game object (maybe one attached to a character or movable object) that would allowed you to instantiate objects at the point the raycast collides with another object, or the ground, or a designated layer... Again, it all depends on how you want to implement the instantiated object into the game world. $$anonymous$$ouse position is a bad route to go though.
You need to figure out a distance in front of your camera you want to instantiate. Then you could so this (10 units in front of the camera):
if (Input.Get$$anonymous$$ouseButtonDown(0)){
var pos = Input.mousePosition;
pos.z = 10.0;
pos = Camera.main.ScreenToWorldPoint(pos);
Instantiate(X,pos, Quaternion.identity); //Creating clones
}