Clone object and move forward.

Hey, I want to put an object in another object (physically, in editor) clone it and then move it forwards. Now, I know I can make it move forwards with something like:

transform.position = new Vector3 (0, 5f, 0, Space.Self);

(or something alike)
But how do I make a clone of an object and select it and make it execute that command?

Look up Instantiate and Start().

use a prefab.
build the object you want in the scene(hierarchy tab) and drag it to some folder in your asset(project tab) folder.
then make a script similar to this:

public class Example
{
    public float speed;
    public GameObject objectToClone;
    private GameObject clonedObject;

    void Start()
    {
        clonedObject = (GameObject)Instantiate(objectToClone, transform.position, transform.rotation);
    }
    void Update()
    {
        if(clonedObject == null)
            return;
        clonedObject.transform.position += Vector3.forward*speed*Time.deltaTime;
    }
}

and drag that prefab in the objectToClone slot in the inspector after you have put the script on a game object

I don’t know why my post didn’t appear. I clearly remember posting it. Whatever:

Your script works great, but it works just once, that’s because script clones only normal object and not a pre-fab.

I have an issue. Now, when there’s a clone moving forward and you make another one move (you initialize another one), the first stops in the air.

using UnityEngine;[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]using System.Collections;[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040][/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]public class CloneANDShot : MonoBehaviour {[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]    public float speed;[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]    public GameObject objectToClone;[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]    private GameObject clonedObject;[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]    public bool bulletGo = false;[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040][/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]    void Start() {[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]        clonedObject = (GameObject)Instantiate(objectToClone, transform.position, transform.rotation);[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]    }[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]    void Update() {[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]        if (Input.GetMouseButtonDown(0)) {[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]            clonedObject = (GameObject)Instantiate(objectToClone, transform.position, transform.rotation);[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]        }[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]        clonedObject.transform.position += Vector3.forward * speed * Time.deltaTime;[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]    }[/COLOR][/COLOR]
[COLOR=#bfbfbf][COLOR=#404040]}

Also, I tried to put entire ball (projectile) and prefab under camera, so the projectile goes in direction of which I’m facing, but it doesn’t work. I tried it by making Hierachy.

You are using the code tags incorrectly. If you want the new object to move as well, you should put the move code to a separate script and attach that to the prefab.