Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do I move GameObjects that was just created from script?

Discussion in 'Scripting' started by myusernameisnthere, May 25, 2017.

  1. myusernameisnthere

    myusernameisnthere

    Joined:
    May 22, 2017
    Posts:
    4
    So what this should do is when you press "q" (which is the FirstSkill), it creates a cube on the top right part of the player (the player is a capsule) and move it to a position in front of the player, pretty much throwing it.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     private float speed = 15f;
    8.     private float jumpForce = 15f;
    9.     private float gravity = 30f;
    10.     private Vector3 moves = Vector3.zero;
    11.     private Vector3 miniMeteorDestination = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z + 15);
    12.     private float miniMeteorSpeed = 30f;
    13.  
    14.  
    15.  
    16.     void Start () {
    17.         Cursor.lockState = CursorLockMode.Locked;
    18.     }
    19.  
    20.     void Update () {
    21.         Stamina staminaScript = transform.GetComponent<Stamina>();
    22.         CharacterController controller = GetComponent<CharacterController>();
    23.         float translation = Input.GetAxis("Vertical")*speed;
    24.         float straffe = Input.GetAxis("Horizontal")*speed;
    25.         translation *= Time.deltaTime;
    26.         straffe *= Time.deltaTime;
    27.  
    28.         transform.Translate(straffe, 0, translation);
    29.  
    30.         if(Input.GetKeyDown("escape")){
    31.             Cursor.lockState = CursorLockMode.None;
    32.         }
    33.         if(Input.GetButtonDown("Jump") && controller.isGrounded){
    34.             moves.y = jumpForce;
    35.         }
    36.         moves.y -=gravity*Time.deltaTime;
    37.         controller.Move(moves *Time.deltaTime);
    38.  
    39.         if(Input.GetButtonDown("Teleport") && staminaScript.stamina > 199){
    40.             transform.Translate(0, 0, 8);
    41.             staminaScript.stamina -= 200;
    42.         }
    43.         if(Input.GetButtonDown("Teleport") && staminaScript.stamina < 200){
    44.             transform.Translate(0, 0, 0);
    45.         }
    46.         if(Input.GetButtonDown("FirstSkill") && staminaScript.stamina > 99){
    47.             staminaScript.stamina -= 100;
    48.             MiniMeteorFunction();
    49.         }
    50.     }
    51.  
    52.     IEnumerator MiniMeteorFunction(){
    53.         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    54.         cube.name = "MiniMeteor";
    55.         cube.transform.position = new Vector3(transform.position.x + 1f, transform.position.y + 1.5f, transform.position.z);
    56.         yield return new WaitForSeconds(0.5f);
    57.         cube.transform.position = Vector3.MoveTowards(cube.transform.position, miniMeteorDestination, miniMeteorSpeed * Time.deltaTime);
    58.     }
    59. }
    60.  
    The errors are:
    Assets/Scripts/PlayerMovement.cs(11,54): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'
    Assets/Scripts/PlayerMovement.cs(11,76): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'
    Assets/Scripts/PlayerMovement.cs(11,102): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'

    Thanks in advance.
     
    Last edited: May 25, 2017
  2. hydudajan

    hydudajan

    Joined:
    Oct 2, 2016
    Posts:
    6
    Hello,
    the error is in line 11, like the message say's. You cant acces the transform property from outside a method.

    put this in line 11.
    private Vector3 miniMeteorDestination = Vector3.zero

    and this in your "Start" method
    miniMeteorDestination = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z + 15);
     
    myusernameisnthere likes this.
  3. myusernameisnthere

    myusernameisnthere

    Joined:
    May 22, 2017
    Posts:
    4
    It had no errors but when I test the game, I press q, the stamina system says that it works, but it doesn't create a cube and it doesn't move towards the miniMeteorDestination. How do I fix this? Thanks.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Are you sure the cube isn't create? It looks correct in the code, I think.

    However, as for the moving portion: Vector3.MoveTowards: https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

    Only moves a part of the way, based on the 3rd parameter. So, even if the cube were being created, it would only move by "speed * Time.deltaTime" , only do that once, and then it's over. You could expand your coroutine to keep moving it, or you could put on a script on the cube that has a destination and updates it, as 2 options.
     
  5. myusernameisnthere

    myusernameisnthere

    Joined:
    May 22, 2017
    Posts:
    4
    I'm sure that the cube isn't appearing whenever I press q on play mode. I don't know why.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    oh my goodness.. :)
    I completely overlooked something.
    Change your method call to : StartCoroutine(MiniMeteorFunction());

    My bad :)
     
  7. myusernameisnthere

    myusernameisnthere

    Joined:
    May 22, 2017
    Posts:
    4
    It actually works! But there's another problem. No matter how I change the z axis position of the miniMeteorDestination, the cube still moves by the same length, even if I add 1000f to the z axis position of miniMeteorDestination it moves just by approximately 1. It's not even moving, it's teleporting. What should I do? Sorry for asking too many questions.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    As I tried to say earlier, it will only move a tiny bit and then stop. You need to expand the coroutine to include that keeps moving it or you could make an update script that is added to the cube that you spawn.
    The docs have a very simple example : https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
    That would have to be modified a bit if it's in a coroutine, but hopefully that can get you pointed in the right direction.