Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiate and move a Transform object

Discussion in 'Scripting' started by Kooth, Mar 10, 2015.

  1. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    Hello !
    I wan't to instantiate a object and move it .
    Actually doesn't work : only a lot of my object spawn.
    Code (JavaScript):
    1.  
    2.  
    3.  
    4.  var cube : Transform;
    5.  
    6.  
    7.  
    8.  
    9.  
    10. function Update () {
    11.  
    12.  var hit : RaycastHit;
    13. // var mousePos : Vector3 = Input.mousePosition;
    14.  var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    15.  
    16.  
    17.  
    18.          
    19.  
    20. if(Input.GetKey(KeyCode.B)) {
    21.     if(Physics.Raycast(ray,hit, 8)) {
    22.          
    23.            if(hit.transform.tag == "terrain" ){
    24.    
    25.    Instantiate(cube,hit.point,Quaternion.identity);
    26.         cube.position = hit.point ;
    27.     }
    28. }
    29.  
    30.  
    31. }
    32.  
    thanks
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    A couple things...

    You are using GetKey which will return true for every frame the key is pressed. You may want to use GetKeyDown or GetKeyUp depending on when you want the event to occur.

    You are changing your prefab's position, not the position of the Instantiated object.
    Code (csharp):
    1. GameObject obj = (GameObject)Instantiate (cube, hit.point, Quaternion.identity);
    2. obj.transform.position = hit.point;
     
  3. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    Hey!
    Thank for reply !
    But i have a problem i add
    Code (JavaScript):
    1.  var obj : GameObject = Instantiate(cube,hit.point,Quaternion.identity);
    but i have a error : Cannot convert 'UnityEngine.Transform' to 'UnityEngine.GameObject'.
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Sorry, I didn't realize that you were using JavaScript and the cube was already a Transform.

    Just change GameObject to Transform in the line you posted.
     
  5. Kooth

    Kooth

    Joined:
    Feb 23, 2015
    Posts:
    53
    No it's me !
    I understand now but i don't have what i wan't .

    Code (JavaScript):
    1. if(Physics.Raycast(ray,hit, 8)) {
    2.          
    3.            if(Input.GetKeyDown(KeyCode.B)) {
    4.          
    5.          
    6.                  if(hit.transform.tag == "terrain" ){
    7.                
    8.   var obj : Transform = Instantiate(cube,hit.point,Quaternion.identity);
    9.    //obj.position = hit.point ;
    10.        
    11.     }
    12. }
    13. if(Input.GetKey(KeyCode.N)) {
    14. obj.position = hit.point ;
    15. }
    16. }


    I wan't when i press B my object instantiates et when i press N my object move and follow my mouse.

    Thanks.
     
  6. Niekos

    Niekos

    Joined:
    Mar 11, 2015
    Posts:
    8
    You could make the object get Input.mouseposition and when you press N set it's position to your mouseposition.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    In JavaScript casting is done using as

    Code (JavaScript):
    1. var obj : Transform = Instantiate(cube,hit.point,Quaternion.identity) as Transform;
     
  8. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    JavaScript and C# handle casting in the same way. You can also use 'as' in C#, just like you can use (Transform)Instantiate (bla) in JavaScript.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can use unsafe casting in JavaScript? That's good to know. I honestly don't pay much attention to the language, I was under the impression it only did safe casting. In that case its better to do this unsafe, like so

    Code (JavaScript):
    1. var obj : Transform = (Transform)Instantiate(cube,hit.point,Quaternion.identity);