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

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.

using System.Collections;
using UnityEngine;


public class PlayerMovement : MonoBehaviour {

    private float speed = 15f;
    private float jumpForce = 15f;
    private float gravity = 30f;
    private Vector3 moves = Vector3.zero;
    private Vector3 miniMeteorDestination = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z + 15);
    private float miniMeteorSpeed = 30f;
 
 

    void Start () {
        Cursor.lockState = CursorLockMode.Locked;
    }
 
    void Update () {
        Stamina staminaScript = transform.GetComponent<Stamina>();
        CharacterController controller = GetComponent<CharacterController>();
        float translation = Input.GetAxis("Vertical")*speed;
        float straffe = Input.GetAxis("Horizontal")*speed;
        translation *= Time.deltaTime;
        straffe *= Time.deltaTime;

        transform.Translate(straffe, 0, translation);

        if(Input.GetKeyDown("escape")){
            Cursor.lockState = CursorLockMode.None;
        }
        if(Input.GetButtonDown("Jump") && controller.isGrounded){
            moves.y = jumpForce;
        }
        moves.y -=gravity*Time.deltaTime;
        controller.Move(moves *Time.deltaTime);

        if(Input.GetButtonDown("Teleport") && staminaScript.stamina > 199){
            transform.Translate(0, 0, 8);
            staminaScript.stamina -= 200;
        }
        if(Input.GetButtonDown("Teleport") && staminaScript.stamina < 200){
            transform.Translate(0, 0, 0);
        }
        if(Input.GetButtonDown("FirstSkill") && staminaScript.stamina > 99){
            staminaScript.stamina -= 100;
            MiniMeteorFunction();
        }
    }

    IEnumerator MiniMeteorFunction(){
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.name = "MiniMeteor";
        cube.transform.position = new Vector3(transform.position.x + 1f, transform.position.y + 1.5f, transform.position.z);
        yield return new WaitForSeconds(0.5f);
        cube.transform.position = Vector3.MoveTowards(cube.transform.position, miniMeteorDestination, miniMeteorSpeed * Time.deltaTime);
    }
}

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.

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);

1 Like

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.

Are you sure the cube isn’t create? It looks correct in the code, I think.

However, as for the moving portion: Vector3.MoveTowards: Unity - Scripting API: Vector3.MoveTowards

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.

I’m sure that the cube isn’t appearing whenever I press q on play mode. I don’t know why.

oh my goodness… :slight_smile:
I completely overlooked something.
Change your method call to : StartCoroutine(MiniMeteorFunction());

My bad :slight_smile:

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.

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 : Unity - Scripting API: Vector3.MoveTowards
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.