Search Unity

How to make an object appear onCLick.

Discussion in 'Scripting' started by Lolifius, Apr 21, 2014.

  1. Lolifius

    Lolifius

    Joined:
    Apr 21, 2014
    Posts:
    5
    Hi everybody,

    I'm starting on unity scripting and I'm trying to create a script that will make an object apear where I click.

    For exemple, one click and BOX1 appear where I click, another click and BOX2 appear where I clicked, etc...

    What is best to use? Knowing that I want to make the boxes appear from an other part of the scene

    Thanks
     
    Last edited: Apr 21, 2014
  2. BeforeTheLight

    BeforeTheLight

    Joined:
    Jan 7, 2011
    Posts:
    168
    Hey Lolifus a little more information would be helpful as to what you are trying to do. But here is what I would do given this information.

    I would create a variable to hold the box you would like to create. Once this line is created and you attach this script to an object then you can drag a prefab into the variable. I would attach this to the camera for ease later on.
    Then I would in the update function call a if mouse button down then call a create object function.

    http://docs.unity3d.com/Documentation/ScriptReference/Input.GetMouseButtonDown.html

    http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html

    http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

    Code (csharp):
    1.  
    2. var box : GameObject;
    3.  
    4. function Update()
    5. {
    6.      if(Input.GetMouseButtonDown(0))//Checks to see if left mouse button was clicked.
    7.      {
    8.           CreateBox();
    9.      }
    10. }
    11.  
    12. function CreateBox()
    13. {
    14.      //Here is why I would attach this to the camera for ease to call this function on the camera.
    15.      //Depending on your camera orientation you may want to change which axis gets mouse position x and y.
    16.      //This is looking straight down z.
    17.      var mousePos : Vector2 = Input.mousePosition;
    18.    
    19.      //Depth you want the center of the object to be is z which I used zero
    20.      var boxPos : Vector3 = camera.ScreenToWorldPoint(mousePos.x, mousePos.y, 0);
    21.      
    22.      //I used the perfab box's rotation here but you can enter what you'd like as a euler using Quaternion.Euler(x,y,z)
    23.      Instantiate(box, boxPos, box.transform.rotation);
    24. }
    25.  
     
  3. Lolifius

    Lolifius

    Joined:
    Apr 21, 2014
    Posts:
    5
    Thanks for your answer. You are right you need more info.

    It looks promising with your code but I forgot to say its 2D. To be precise on what I want to do. Let's say I have 3 2D objects. When I click on the game I want the first to appear where I click. When I click asecond time I want the second object to appear, etc...
     
    Last edited: Apr 21, 2014
  4. BeforeTheLight

    BeforeTheLight

    Joined:
    Jan 7, 2011
    Posts:
    168
    ok well in that case its a little easier. In 2d you know your z I normally use 0 but what ever you'd like. You could do this a few ways in the example you gave I would use a int variable and increment it each time you click. I would have an array of objects that I would call from using your integer variable. I don't know how many objects you have but this example goes through your array and then restarts at 0 when you reach the end.

    Code (csharp):
    1.  
    2. //Changed to an array of objects assign as many as you have in inspector
    3. var boxes : GameObject[];
    4. var boxCounter : int;
    5.  
    6. function Update()
    7. {
    8.      if(Input.GetMouseButtonDown(0))//Checks to see if left mouse button was clicked.
    9.      {
    10.           CreateBox();
    11.      }
    12. }
    13.  
    14. function CreateBox()
    15. {
    16.      var mousePos : Vector2 = Input.mousePosition;
    17.  
    18.      //Depth you want the center of the object to be is z which I used zero
    19.      var boxPos : Vector3 = camera.ScreenToWorldPoint(mousePos.x, mousePos.y, 0);  
    20.  
    21.      //I used the perfab box's rotation here but you can enter what you'd like as a euler using Quaternion.Euler(x,y,z)
    22.      Instantiate(boxes[boxCounter], boxPos, boxes[boxCounter].transform.rotation);
    23.      
    24.      //This will increment if there are more boxes or reset to 0 if it is the last one.
    25.      if(boxCounter == boxes.length-1)
    26.      {
    27.           boxCounter = 0;
    28.      }else{
    29.           boxCounter ++;
    30.      }
    31. }
    32.  
     
  5. Lolifius

    Lolifius

    Joined:
    Apr 21, 2014
    Posts:
    5
    Thanks very much for this.

    But I'm not succeding at making it work, do you have an idea of what i need to do/add so it will work ?
     
  6. BeforeTheLight

    BeforeTheLight

    Joined:
    Jan 7, 2011
    Posts:
    168
    I have not tried this just scripted it up in the message but will give it a go and make sure it works tonight. Just a quick check you have created a JavaScript file and inserted the code above into it. Then dragged it onto the camera object then added your prefabs to the array in the inspector. If what I just said doesn't make sense see https://unity3d.com/learn/tutorials/modules to get the basics.
     
  7. Lolifius

    Lolifius

    Joined:
    Apr 21, 2014
    Posts:
    5
    I did the JS file, I put it on the camera but I don't know where is the array in the inspector (btw I also change this which I think was wrong about Vector3 missing camera.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, 0)) )
     
  8. BeforeTheLight

    BeforeTheLight

    Joined:
    Jan 7, 2011
    Posts:
    168
    I tested this last night but did not have internet to respond to you. You are correct I forgot the Vector3() inside the screen to world point function. But everything else was good. If you do not know how to assign variables you really should watch the beginner tutorials I linked to earlier in this conversation but the video below will show you how to assign objects to an array in the inspector. Around 2 minutes starts the array assign. Please do a little work on learning the workflow of unity it will better help you ask questions and get better answers from those questions.

    https://unity3d.com/learn/tutorials/modules/beginner/editor/the-inspector
     
  9. Lolifius

    Lolifius

    Joined:
    Apr 21, 2014
    Posts:
    5
    Oh well I just didn't change the Size item !!

    It's working thx !